Post Reply 
(12C) Combination and Permutation
08-19-2017, 05:26 PM (This post was last modified: 08-19-2017 06:00 PM by Dieter.)
Post: #10
RE: (12C) Combination and Permutation
(08-17-2017 09:49 PM)BartDB Wrote:  The C(n,r) routine is based on the identity C(n,r) = C(n,n-r), selecting the minimum value of r and n-r for the calculations.

That's a good idea that can significantly speed up the calculation. But your program chooses the maximum instead of the minimum. ;-) Swap steps 03 and 04 and the program should work as intended.

(08-17-2017 09:49 PM)BartDB Wrote:  In the loop body, the division and multiplication are done alternately to avoid overflows in cases where the intermediate calculations exceed the dynamic range of the HP-12C. Since doing division can result in non-integer results, the final result is rounded to the nearest integer.

The result is not rounded but truncated. If you want to round you should add a small amount like 0,1 or 0,3 before INTG trims off the decimals.

But you can address the roundoff problem by counting the denominator up instead of down. This yields integer results after each division. On the other hand overflow may occur a little bit earlier, but this can be handled by initializing R3 with 0,001 instead of 1 and finally multiplying the result by 1000.

(08-17-2017 09:49 PM)BartDB Wrote:  The P(n,r) routine is based on the identity P(n,r) = n x (n-1) x (n-2) x ... x (n-r+1).

That's a nice and very compact routine. But does it also return correct results for r=0 ?
Maybe this works better:

Code:
.. ...
25 CHS
26 x<>y
27 +
28 LstX
29 1
30 STO 3
31 R↓
32 x≤y?
33 GTO 22
34 STO*3
35 1
36 -
37 GTO 32

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


Messages In This Thread
(12C) Combination and Permutation - BartDB - 08-17-2017, 09:49 PM
RE: (12C) Combination and Permutation - Dieter - 08-19-2017 05:26 PM



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