(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: .. ... Dieter |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(12C) Combination and Permutation - BartDB - 08-17-2017, 09:49 PM
RE: (12C) Combination and Permutation - Gamo - 08-18-2017, 08:28 AM
RE: (12C) Combination and Permutation - pier4r - 08-18-2017, 08:55 AM
RE: (12C) Combination and Permutation - BartDB - 08-18-2017, 04:58 PM
RE: (12C) Combination and Permutation - pier4r - 08-18-2017, 05:12 PM
RE: (12C) Combination and Permutation - Gamo - 08-18-2017, 11:36 AM
RE: (12C) Combination and Permutation - pier4r - 08-18-2017, 12:50 PM
RE: (12C) Combination and Permutation - Joe Horn - 08-18-2017, 02:54 PM
RE: (12C) Combination and Permutation - pier4r - 08-18-2017, 03:18 PM
RE: (12C) Combination and Permutation - Dieter - 08-19-2017 05:26 PM
RE: (12C) Combination and Permutation - Gamo - 03-15-2020, 11:37 AM
|
User(s) browsing this thread: 9 Guest(s)