HP15c program: Calculate y mod x
command display
f LBL B 001-42,21,12
STO 3 002- 44 3 (store X in reg3)
x><y 003- 34
STO 4 004- 44 4 (store Y in reg4)
x><y 005- 34
divide 006- 10
g int 007- 43 44
RCL 3 008- 45 3
* 009- 20
CHS 010- 16
RCL 4 011- 45 4
+ 012- 40
g RTN 013- 43 32
This program uses the following label: LBL B
The program uses the registers (STO/RCL): 3 and 4
Using the program
The modulus of Y mod X is an integer number smaller than X and
the reminder of y divided by x. Enter Y and X and then press "GSB B"
The modulus is the number that is in X after the execution of the program.
The algorithm
The HP15c has an integer function to convert a floating point number into
an integer number. We can use this to calculate the modulus:
y mod x = y - (x * int( y/x ))
Example to test the program:
15 mod 6 = 3
15
Enter
6
GSB B
Expected result (shown in the display):
3
Alternate implementation that uses only the stack
command display
f LBL B 001-42,21,12
x><y 002- 34
Enter 003- 36
R down 004- 33
x><y 005- 34
/ (divide) 006- 10
g Last X 007- 43 36
x><y 008- 34
g int 009- 43 44
* 010- 20
x><y 011- 34
R down 012- 33
- 013- 30
g RTN 014- 43 32
This program uses the following label: LBL B
The program uses the registers (STO/RCL): none
© Guido Socher