Post Reply 
HP 12C Permutation and Combination
08-23-2017, 06:32 PM (This post was last modified: 05-07-2018 08:28 PM by Dieter.)
Post: #22
RE: HP 12C Permutation and Combination
(08-23-2017 06:40 AM)Gamo Wrote:  Your program is very nice and easy to use so I use it a lot and find out another problem with 60p5 this time the result show 655 381 440.2 on regular cheap Casio fx-260 solar gave result 655 381 440

Sure. As already explained (please read my previous posts again) this can and will happen if you calculate with factorials which cannot be represented exactly with 10 digits.

(08-23-2017 06:40 AM)Gamo Wrote:  Is it possible just to add INTG to the program?

That's not a good idea. The error can be both positive and negative. If the result is a bit too small, INTG will be off by one (or even more). Just remember 25P7 on the Prime or any other correctly working 12-digit calculator (2422727999,99) resp. on the 10-digit 12C (2422727999). You could round the result by adding 0,5 first and then use INTG, but this does not mean that the result is exact – the error may be > 1.

If you want to avoid or at least minimize this kind of error you have to do the calculation without factorials. I have described a way to do so and you'll also find an explanation why the preferable method avoids roundoff errors.

Here are two programs that will do this job.

First, here is a compact version that returns both nCr in X and nPr in Y. If you want to have it the other way round, simply swap the final RCL 3 and RCL 4.

Code:
01  STO 0
02  x<>y
03  STO 2
04  1
05  STO 1
06  STO 3
07  STO 4
08  RCL 0
09  x=0?
10  GTO 21
11  RCL 2
12  STO×3
13  STO×4
14  RCL 1
15  STO÷4
16  1
17  STO-0
18  STO+1
19  STO-2
20  GTO 08
21  RCL 3
22  RCL 4
23  GTO 00
Edit: corrected an error several months after posting. #-)

25 [ENTER] 7 [R/S] => 480700   [x<>y] => 2422728000

This program should (usually) return accurate results if the latter can be respresented exactly with 10 digits.

Second, here is a deluxe version that calculates nCr and nPr. A negative r yields nPr, a positive r gives nCr:

25 [ENTER]  7 [R/S] => 480700
25 [ENTER] -7 [R/S] => 2422728000

Code:
01  ENTER
02  EEX
03  3
04  CHS
05  STO 3
06  CLX
07  x≤y?
08  GTO 19
09  +
10  x<>y
11  +
12  LstX
13  x≤y?
14  GTO 43
15  STO×3
16  1
17  -
18  GTO 13
19  +
20  x<>y
21  STO 2
22  x<>y
23  -
24  LstX
25  x≤y?
26  x<>y
27  x<>y
28  STO 0
29  1
30  STO 1
31  RCL 0
32  x=0?
33  GTO 43
34  RCL 2
35  STO×3
36  RCL 1
37  STO÷3
38  1
39  STO-0
40  STO+1
41  STO-2
42  GTO 31
43  EEX
44  3
45  STO×3
46  RCL 3
47  GTO 00

Ths program uses a trick (as proposed in the other thread I linked to earlier) to calculate nCr without overflow as long as the result does not exceed 9,999999999 E+99. Of course this also applies to nPr. There is no overflow unless the result overflows.

Both programs will correctly return 60 P 5 = 655381440. This is because they do not work with factorials but with repeated multiplication. Just as probably all (or at least most) calculators do in their internal nPr and nCr functions.

Of course this method is slower than calculating with factorials. But on a 12CP, let alone an emulator, this should not be an issue.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP 12C Permutation and Combination - Gamo - 08-15-2017, 04:54 AM
RE: HP 12C Permutation and Combination - Dieter - 08-23-2017 06:32 PM



User(s) browsing this thread: 1 Guest(s)