Re: 15c: Need good programs for Mod, nCr, nPr Message #30 Posted by C.Ret on 16 Sept 2011, 4:47 a.m., in response to message #21 by Katie Wasserman
Thank you Katie and Marcus.
It’s now clear for me, and I realize that there is no chance that complexcalculus capabilities of the HP15c will help there.
Katie’s code translate to RPL will be same sort of :
« DUP2 / IP * »
Here follow illustration of the various clever stack moves induce by RPN and RPL code:
At first consider the clever stack manipulation from Katie’s contribution:
[ 0 ][ Enter ][Cmplx][ + ][ / ][ IP ][ * ][  ]
Suppose we have y = x.q + r, where integer q is the multiplicand and integer r the modulus of interest. We have r= y MOD x. And I also need posing f = (y/x) – INT(y/x) = FRAC( y/x ).
I like the clever way of managing the RPN stack:
t: ~  ~  y  y  y  y  y  y 
z: ~  y  x  x  y  y  y  y 
y: y  x  0  y+0  x  x  y  y 
x: x  0  0  x+0  y/x  q  x.q y MOD x
[0] [ENTER] [Cplx+] [/] [IP] [*] []
It’s really close to what may be done in RPL (without the builtin MOD function.
4:  y     
3:  x  y  y   
2: y  y  x  x  y  
1: x  x  y/x  q  x.q y MOD x
« [DUP2] [/] [IP] [*] [] »
The version proposed by flsh, which look really close to a HP41C style with stack register manipulation.It spare one step through memory recall and operation combine in one [ RCL/ IND Y ] instruction.
t: ~~~  ~  ~  ~  ~  ~ 
z: ~  y  y  y  ~  ~ 
y: y  x  x  x  y  ~ 
x: x  y  y/x  q  x.q  y MOD x 
[RCl ST Y][RCL/ ST Y] [IP] [*] []
But as RPL devices, the HP41C (and HP42S) calculator has builtin MOD function.
Here a version for the great WP34S which have no builtin MOD function (or I am missing something?):
t: ~  y  y  y  y  y 
z: ~  x  y  y  y  y 
y: y  y  x  x  y  y 
x: x  x  y/x  q  x.q  y MOD x 
[^{C}ENTER] [ / ] [FLOOR] [*] []
How to produce with short code the same stack manipulations on the HP15C is another storie.
Here is for comparison the way proposed by Gerson:
l: ~  ~  ~  ~  ~  x  x  x  x  x  y/x  y/x  y/x 
t: ~  ~  ~  y  y  y  y  ~  y  y  y  y  y 
z: ~  ~  x  ~  ~  y  y  y  y  y  y  y  y 
y: y  x  y  x  y  ~  y/x  y  y/x  x  x  y  y 
x: x  y  y  y  x  y/x  ~  y/x  x  y/x  q  q.x  r 
x<>y ENTER Rdwn x<>y / x<>y Rdwn LASTx x<>y INT * 
Obviously HP15C lacks same stack manipulation command. But that’s what makes the charm of this oldstyle true RPN calculator. In daily use of it, did this lack of complex stack manipulations miss any users?
A way to mimic the DUP2 stack manipulation style will be to use statistic function. But, even if this is short, this solution will clear (and use) registers R2 to R7. Perhaps it is not a convenient way ?
R2: ~  0  0  0  1  1  1  1  1  1  1 
R3: ~  0  0  0  x  x  x  x  x  x  x 
R4: ~  0  0  0  x.x  x.x  x.x  x.x  x.x  x.x  x.x 
R5: ~  0  0  0  y  y  y  y  y  y  y 
R6: ~  0  0  0  y.y  y.y  y.y  y.y  y.y  y.y  y.y 
R7: ~  0  0  0  x.y  x.y  x.y  x.y  x.y  x.y  x.y 
t : ~  0  0  0  0  0  y  y  y  y  y 
z : ~  0  0  0  0  y  x  y  y  y  y 
y : ~  0  y  y  y  y  y  x  x  y  y 
x : ~  O  y  x  1  x  x  y/x  q  q.x  r 
[CLEAR Sig] y [ENTER] x [Sig+][RCL Sig][RCL Sig] [/] [IP] [*] []
Since this last solution use so much memory register and clearing statistic also clear the stack, it is not a valid code.
I would prefer a simple one normaly using only one register and making the job straighforward.
I greatly prefer not to try to manipulate stack on an HP15c, and using an approximative MOD function, only valid for y and x both positive :
l: ~  x  x  x  y/x  y/x 
t: ~  ~  ~  ~  ~  ~ 
z: ~  ~  ~  ~  ~  ~ 
y: y  ~  y/x  x  x  ~ 
x: x  y/x  x  y/x  f  r 
[FIX 0] [ ÷ ] [LASTx] [x<>y] [FRAC] [ × ]
Edited: 16 Sept 2011, 4:51 p.m. after one or more responses were posted
