15c: Need good programs for Mod, nCr, nPr  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: 15c: Need good programs for Mod, nCr, nPr (/thread195228.html) 
15c: Need good programs for Mod, nCr, nPr  Dave Britten  09152011 From the look of the tracking info, my 15c LE will be showing up this afternoon. I noticed from staring at photos of a 15c keyboard, that it's lacking Mod, nCr, and nPr functions, which I do use on a fair basis on my 48SX. Are there any good implementations of these functions for a 15c? I'd like something that will produce results without overflowing during an intermediate step if one of the inputs is relatively large (the 48SX will compute COMB(3000, 4) with ease, for example, and that won't fly with a naive implementation). Speed won't be an issue with the LE model, I presume. :)
Re: 15c: Need good programs for Mod, nCr, nPr  Martin Pinckney  09152011 The 15c has them. Cy,x Py,x. "+" key.
Re: 15c: Need good programs for Mod, nCr, nPr  Dave Britten  09152011 Ah, excellent. Can't believe I missed that.
It appears I'm still in need of a Mod function, however.
Re: 15c: Need good programs for Mod, nCr, nPr  Don Asmus  09152011 Here is a small 15C MOD routine that works if both arguments have the same sign. / LSTx x<>y FRAC *
It was verified against 48GX results. If the signs of both arguments do not match, it does not return the correct result. If you are like me, and only use arguments of the sign sign, it will be fine. I suspect that a proper algorithm uses the Floor function, or something similar that is also not natively available on the 15C.
Re: 15c: Need good programs for Mod, nCr, nPr  x34  09152011 HP15C has INT(IP) and FRAC(FP). So, X MOD Y could be XY*IP(X/Y) (depending on modulo function definition).
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09152011 Yes. Only missing the MOD function which may be easy to program.
#  Thank you to Torsten for pointing me out to his HP15C emulator (it makes easy to copypaste program listing). Please note that this code use no register and leave the contents of t and z register intact in the stack (only moved down one step).
Edited: 15 Sept 2011, 2:30 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Dave Britten  09152011 Just found the Mod routine I made for the 32s/32sii (and had forgotten about). It's got the interesting quality that it preserves stack contents, and only needs one storage register. It's also a little more impervious to precision issues than the implementations that use FRAC *. This ought to work with only minimal conversion effort.
M01 LBL M
Anybody want to take a stab at making it better?
Re: 15c: Need good programs for Mod, nCr, nPr  Egan Ford  09152011 You'll need to FIX 0 first, e.g. 10 3 MOD is not 1, its .9999999999.
Re: 15c: Need good programs for Mod, nCr, nPr  Gerson W. Barbosa  09152011
Quote:# Note: accuracy is only a result of display rounding process !
001 LBL B This needs some simplification and stack preserving, but you've got the idea.
Re: 15c: Need good programs for Mod, nCr, nPr  Egan Ford  09152011 LBL 0This will just subtract off until it cannot. Slow, but short, no registers, probably very fast on 15LE. y > x. Edited: 15 Sept 2011, 2:54 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Thomas Radtke  09152011 Very impressive demonstration of the beauty of RPN (and your knowledge of it).
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09152011 Quote: You are right, but FIX 4 may give in most cases the right integer result. Only FIX 9 may encounter a wrong result in rare occasions (like the one you give in your exemple).
Since there is no STD display format, most FIX n may be OK with n less than 9.
Re: 15c: Need good programs for Mod, nCr, nPr  Katie Wasserman  09152011 I came up with this one on the 32sii a long while back, it's not so obvious but is short and sweet and doesn't need more than the stack. for y mod x:
0 Katie
Edited: 15 Sept 2011, 3:30 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09152011 Hi, Yes, you are right, the first version of my code return an approximate y MOD x since it is not (or in rare situation) an integer, as expected. So, I propose a second version which returns the correct integer result as long as both arguments are both positives. My new version use one register to store a the small value eps = 5× 10^{7} used to correctly roundup results. This eps constant have to be adjusted for larger result than 10000.
# USAGE : Initiate register R0 with a small positive value (i.e. 5× 10^{7}), Enter integer y and x into the HP15 LE stack, Press [ f ][ A ] to run the code. The integer result y MOD x is return and displayed.
Edited: 15 Sept 2011, 4:07 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09152011 Quote:
Really nice, thank you, much better thant the one I proposed !
#  But, don't try 789456123 MOD 9 on an original HP15c, it's so loooooong for a really expected result !
Edited: 15 Sept 2011, 4:06 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  fhub  09152011 Quote:This is even a bit shorter: RCL YI hope the 32s has RCL Y and RCL/ Y, I don't know this calculator. ;) Re: 15c: Need good programs for Mod, nCr, nPr  Katie Wasserman  09152011
Quote:
I wish it did, but no it doesn't have those operations.
Re: 15c: Need good programs for Mod, nCr, nPr  fhub  09152011 Quote:That's strange, because in message #6 Dave has used STO T and RCL T in a 32s program, so I couldn't imagine that such commands won't exist for register Y !? Re: 15c: Need good programs for Mod, nCr, nPr  Don Shepherd  09152011 But your Y is stack Y. Dave's T is regular register T (of AZ).
Re: 15c: Need good programs for Mod, nCr, nPr  fhub  09152011 Quote:Ahhh, now I got it: then this 32s doesn't have the usual 4 register stack XYZT !? Re: 15c: Need good programs for Mod, nCr, nPr  x34  09152011 Better use INT instead of FRAC, no need to round.
Re: 15c: Need good programs for Mod, nCr, nPr  Katie Wasserman  09152011 Wow this is an unnecessarily confusing discussion. Let's call x,y,z,t the stack and the nonstack registers A, B, C, .... X, Y, Z. The routine I wrote uses the x,y,z,t stack registers only. The routine that Dave wrote uses the stack resisters plus register T. Although it preserves the stack registers, while mine does not. On the 32s/ii there are no STO/RCL functions that allow you directly address x,y,z,t like there are in the 42s, for example. Does that clear things up?
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09152011 Quote:
Re: 15c: Need good programs for Mod, nCr, nPr  Massimo Gnerucci (Italy)  09152011
Quote:
Well, maybe "queen" enough would be more appropriate! Re: 15c: Need good programs for Mod, nCr, nPr  Marcus von Cube, Germany  09152011 If I understand it correctly, 0 ENTER pushes a complex zero to X,Y and the arguments for mod to Z,T. The COMPLX+ just adds 0 to Z and 0 to T, placing the "sum" in X, Y without dropping Z and T. So the result of the operation is that you have the arguments twice on the stack. In RPL you would do DUP2 instead.
Re: 15c: Need good programs for Mod, nCr, nPr  Katie Wasserman  09152011 Exactly how I think of it: 0, ENTER, CMPLX+ is the DUP2 operation, a function in RPL and often defined in Forth. I wish it would have been an RPN function too, I'd certainly use it more than rollup.
Re: 15c: Need good programs for Mod, nCr, nPr  Paul Dale  09152011 It is there on the 34S :)
 Pauli
MOD(y,x) as equivalent to MOD on the HP41/42S and Rmdr on the HP33s  Gerson W. Barbosa  09152011 001 LBL A MOD(y,x) = x  y * Floor(x/y) Perhaps someone might want to do some optimization.
Edited: 15 Sept 2011, 8:55 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Gerson W. Barbosa  09152011 Quote:
You are right. Some Floor function implementations for the HP32SII here. They might work on the HP15C as well.
Re: 15c: Need good programs for Mod, nCr, nPr  Walter B  09162011 Simply use ^{C}ENTER on the WP 34S. And yes, it's RPN :)
Re: 15c: Need good programs for Mod, nCr, nPr  Katie Wasserman  09162011 Thanks for that guys! But I did say
Quote: I really needed this for the past 39 years, every since I had my HP35.
Re: 15c: Need good programs for Mod, nCr, nPr  Howard Owen  09162011 Quote: No problem! Set up a loop with the 34S AT (alter time) function. Decrement the current date until you reach the desired time, leave off a 34S (not the one you are using!) for your former self, and execute BTF (back to future.)
There's very little RPN isn't capable of. :)
Re: 15c: Need good programs for Mod, nCr, nPr  Walter B  09162011 Apologize I didn't have the knowledge then already d:)
Re: 15c: Need good programs for Mod, nCr, nPr  Walter B  09162011 No need for loops  the counterpart of BTF is ATP (Advance To Past, please see page 17 of the well known manual) :)
Edit: Almost forgot  there's also a command Stay At Present time ;) Edited: 16 Sept 2011, 2:28 a.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Howard Owen  09162011 Quote: How does that differ from the 41C stop time (STOPT) x function?
Re: 15c: Need good programs for Mod, nCr, nPr  Walter B  09162011 AFAIK you cannot compare SAP with anything else ...
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09162011 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,
I like the clever way of managing the RPN stack: t: ~  ~  y  y  y  y  y  y 
It’s really close to what may be done in RPL (without the builtin MOD function. 4:  y      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: ~~~  ~  ~  ~  ~  ~  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  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  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 
Since this last solution use so much memory register and clearing statistic also clear the stack, it is not a valid code. 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 
Edited: 16 Sept 2011, 4:51 p.m. after one or more responses were posted
Re: 15c: Need good programs for Mod, nCr, nPr  Paul Dale  09162011 For the 34S:
cmplx ENTER Which is a direct copy of the 32sii version changing IP for FLOOR (the 34S having both). The 34S has a remainder function that is the same as MOD for positive arguments but slightly different for negative.
Re: 15c: Need good programs for Mod, nCr, nPr  Marcus von Cube, Germany  09162011 Some incarnations of the 34S software have a bug that make cENTER unreachable from the keyboard. It's fixed in both the zip and SVN now.
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09162011 Thank you.
I just revised my previous post incorporating the WP34S version. Re: 15c: Need good programs for Mod, nCr, nPr  Paul Dale  09162011 The 34S RMDR operation (hshift divide) behaves the same as C's MOD operator (for both integer and real modes). For example:
7 3 RMDR = 1 There was mention a while ago of this not being the correct behaviour for MOD. I don't remember the exact details though. What should the values be here?
 Pauli
Edited: 16 Sept 2011, 6:13 a.m.
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09162011 Quote:
Hepp!
7 3 RMDR = 1 RMDR is MOD correct since 7 = 2 × 3 + 1 and 0 < 1 < 3 7 3 MOD = 1
Sametime I wonder myself, why and how simple idea became so complicated in the mathematic world ?
Re: 15c: Need good programs for Mod, nCr, nPr  Gerson W. Barbosa  09162011 My (outdated) wp34s manual says: RMDR Equals MOD on HP42S and RMD on HP16C.
However, on the HP42S we have: 7 3 MOD = 1 Gerson.
Re: 15c: Need good programs for Mod, nCr, nPr  Paul Dale  09162011 Isn't the code above doing the remainder instead of MOD then? 7 mod 3 is 1 according to the code above:
[RCl ST Y][RCL/ ST Y] [IP] [*] []
You need FLOOR instead of IP or INT to get a MOD don't you?
Re: 15c: Need good programs for Mod, nCr, nPr  Paul Dale  09162011 The manual is incorrect. It is the same as RMD on the 16C. It honours the sign of the numerator. To get MOD, add the denominator to the result if the signs of the two numbers are different. Maybe I should just implement MOD as well although it will eat up a bit of space to do so. RMDR works with integers, reals and complex numbers and double precision integers  MOD should also.
Re: 15c: Need good programs for Mod, nCr, nPr  Alexander Oestert  09162011 Quote: Katie's code kind of 'ported' to the 15C:
001 LBL A A bit longer but an implementation of the same idea... ;)
Edited: 16 Sept 2011, 9:19 a.m. after one or more responses were posted
Re: 15c: Need good programs for Mod, nCr, nPr  Gerson W. Barbosa  09162011 This is the remainder of the integer division, however, not the modulus function. MOD can be defined as
MOD(x,y) = x  y * Floor(x/y)
where Floor(x) = INT(x) + INT(FRAC(x) + 1)  1
Re: 15c: Need good programs for Mod, nCr, nPr  Alexander Oestert  09162011 As I wrote, my code was just an implementation of Katie's very elegant 32sidea ported to the 15c, whether it yields modulo or remainder  I didn't investigate. After I read the modulo article on wikipedia I'm not sure if I still know the difference between modulo and remainder... ;) Re: 15c: Need good programs for Mod, nCr, nPr  Gerson W. Barbosa  09162011 Yes, there is some confusion. On the HP33s, for instance, they have implemented the modulus function, and yet they name it Rmdr. When the signs of the arguments are the same, they are equivalent:
7 3 RMDR = 1 Re: 15c: Need good programs for Mod, nCr, nPr  Dieter  09162011 Of course there must be no RCL IND Y commands in any of these routines. This would recall the register whose address is in y. RCL YAlthough not required on a HP41 (MOD is available), a double RCL Y will match the DUP2 of RPL: RCL YOf course you can also do it on a classic HP machine. The following code is two steps shorter than Gerson's and uses the tduplication on stack drop: ENTER^The HP35s finally has a RMDR function and can also access the stack registers directly. It features both IP as well as INTG. It even offers a command for integer division, similar to DIV in some programming languages. Very handy in many cases. Using equation mode one could do it this way: REGYREGX*IDIV(REGY,REGX)Dieter Re: 15c: Need good programs for Mod, nCr, nPr  Dieter  09162011 Well... Quote:
...the 35s has them both: IP and INTG. And RMDR, of course. ;) REGYREGX*IP(REGY/REGX)resp. REGYREGX*INTG(REGY/REGX)will return the one or the other result. The second version is equivalent to the 35s RMDR function.
Dieter
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09162011 Thank you for your attentive reading and pointing out the “IND” error. Of course, there’s no indirect access there. My intention was to make HP41C "STO Y  where Y is in stack " notation not confused with HP32 alphabetic name f register (where "STO Y" means store in register named Y). Using reserved keyword IND is my own mistake. I have to correct this in my post. I like your code using a dump zero addition " Clx + " to realize the DUP2 style algorithm on classic. It is a triky way.
t: ~  ~  y  y  y  x  x  x  x  x  x  Nice shot ! Achievable on all the classic !
Edited: 16 Sept 2011, 4:24 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Dieter  09162011 By the way... Quote:FLOOR (or INTG on the 35s) really is a very useful function. However, most devices only offer INT or IP which simply cuts off the decimals.
But there is a way  for instance on the '41: ENTER^You can also try this with 1 and see what you get. Dieter
Edited: 16 Sept 2011, 5:01 p.m.
Re: 15c: Need good programs for Mod, nCr, nPr  C.Ret  09162011 Let make a table to check which fonction for which result :
y x : y MOD x y RMDR x : yx.INT(y/x) yx.FLOOR(y/x) yx.CEIL(y/x)Obviously, MOD is obtained from FLOOR and RMDR from IP or INT. CEIL lead to a third type of modulus.
Re: 15c: Need good programs for Mod, nCr, nPr  Dieter  09182011 I think it's important to say which MOD or RMDR you refer to. For instance, the results of MOD on the 41 and RMDR on the 35s are exactly the same.
Regarding CEIL: usually this is an easy one as CEIL(x) = FLOOR(x). ENTER^On a 35s, use RDMR instead. ;) Or simply CHS INTG CHS. That's one of the things I like on the 35s. Dieter
Edited: 18 Sept 2011, 6:54 a.m.
Re: 15c: Need good programs for Mod, nCr, nPr  Dieter  09182011 C.Ret wrote: Quote:There indeed is a special keyword on those calculators that feature both lettered registers and stack access, e.g. the 42s. Here, a simple "ST" distinguishes between stack and variables:
Recall variable (data register) "Y": RCL YRecall stack register y: RCL ST YOn other calculators RCL Y is unambiguous since it can have only one meaning: There are no lettered data registers or variables on the 41, so RCL Y refers to the stack. On the 32s, there is no direct stack access, so it's clear that RCL Y refers to the variable (data register) with that name. BTW, the 34s does it in a very special way: the content of the stack registers is stored in registers with the same letter. So RCL Y means both "recall variable Y" and "recall stack register Y". ;) Dieter
Edited: 18 Sept 2011, 7:21 a.m.
