(12C) Combination and Permutation

08172017, 09:49 PM
(This post was last modified: 08172017 10:05 PM by BartDB.)
Post: #1




(12C) Combination and Permutation
C(n,r) and P(n,r) for HP12C.
This should work for all cases where the result is less than 1E100. The C(n,r) routine is based on the identity C(n,r) = C(n,nr), selecting the minimum value of r and nr for the calculations. 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 HP12C. Since doing division can result in noninteger results, the final result is rounded to the nearest integer. The P(n,r) routine is based on the identity P(n,r) = n x (n1) x (n2) x ... x (nr+1). To use the C(n,r) routine, position the program counter at step 0 if it's not there already, key in n, hit enter, key in r and hit R/S. To use the P(n,r) routine, position the program counter at step 25 (GTO 25), key in n, hit enter, key in r and hit R/S. Code: 01  ; X=nr Accuracies (wrong didgits are underlined) Combinations
\begin{array}{lrr}\hline Inputs & HP12C & Wolfram Alpha \\\hline 15C5 & 3003 & 3003 \\\hline 36C7 & 8347680 & 8347680 \\\hline 90C7 & 747137556\underline{7} & 7471375560 \\\hline 101C6 & 126733992\underline{1} & 1267339920 \\\hline 70C8 & 94403509\underline{38} & 9440350920 \\\hline 425C23 & 5.9872995\underline{10}E37 & 5.987299532821716... E37 \\\hline 200C55 & 7.7183408\underline{23}E49 & 7.718340811430957... E49 \\\hline 335C167 & 3.044358\underline{801}E99 & 3.044358793146979... E99 \\\hline \end{array} Permutations
\begin{array}{lrr}\hline 25P7 & 2422728000 & 2422728000 \\\hline 99P5 & 8582777280 & 8582777280 \\\hline 100P45 & 7.35060259\underline{4}E84 & 7.350602595423500... E84 \\\hline 5000P27 & 6.94462245\underline{9}E99 & 6.944622452543926... E99 \\\hline \end{array} 

08182017, 08:28 AM
Post: #2




RE: (12C) Combination and Permutation
Hello BartDB
Your program is very good, keep the good work !! Gamo 

08182017, 08:55 AM
(This post was last modified: 08182017 08:56 AM by pier4r.)
Post: #3




RE: (12C) Combination and Permutation
Just giving another comparison (against the sharp el506w with built in functions)
Accuracies (wrong didgits are underlined) Combinations
\begin{array}{lrr}\hline Inputs & HP12C & \texttt{Sharp el506w} \\\hline 15C5 & 3003 & 3003 \\\hline 36C7 & 8347680 & 8347680 \\\hline 90C7 & 747137556\underline{7} & 7471375560 \\\hline 101C6 & 126733992\underline{1} & 1267339920 \\\hline 70C8 & 94403509\underline{38} & 9440350920 \\\hline 425C23 & 5.9872995\underline{10}E37 & 5.98729953\underline{3}E37 \\\hline 200C55 & 7.7183408\underline{23}E49 & \texttt{error} \\\hline 335C167 & 3.044358\underline{801}E99 & \texttt{error} \\\hline \end{array} Permutations
\begin{array}{lrr}\hline 25P7 & 2422728000 & 2422728000 \\\hline 99P5 & 8582777280 & 8582777280 \\\hline 100P45 & 7.35060259\underline{4}E84 & 7.350602595E84 \\\hline 5000P27 & 6.94462245\underline{9}E99 & 6.94462245\underline{3}E99 \\\hline \end{array} Wikis are great, Contribute :) 

08182017, 11:36 AM
(This post was last modified: 08182017 11:36 AM by Gamo.)
Post: #4




RE: (12C) Combination and Permutation
HP Prime on CAS mode
5000 P 27 result 6944622452543926879457210267031870488447849889838527414579105132823700771109934699558993920000000000 same result from WolfarmAlpha. HP Prime calculate just like a real computer computation this is a very capable calculator ever!!! Gamo 

08182017, 12:50 PM
Post: #5




RE: (12C) Combination and Permutation
The hp 50g does the same (and I guess all the math environments with exact computations/big integers , as long as the memory is enough).
Wikis are great, Contribute :) 

08182017, 02:54 PM
(This post was last modified: 08182017 02:56 PM by Joe Horn.)
Post: #6




RE: (12C) Combination and Permutation
(08182017 12:50 PM)pier4r Wrote: The hp 50g does the same (and I guess all the math environments with exact computations/big integers , as long as the memory is enough). Actually, both the 50g and Prime have builtin limitations on large integers, in different ways. The 50g doesn't allow integer exponents greater than 9999 (e.g. 2^9999 works but 2^10000 says "^ Error: Integer too large"). However, the 50g allows the integers themselves to be as big as available memory (as you said), e.g. 2^9999 can be squared without error, and squared again, and so on, until you run out of memory. Prime however imposes a maximum size on large integers themselves, regardless of available memory. It seems to be 2^85991, but I've not seen that documented anywhere. Try this in CAS on your Prime: \[\left ( 2^{8598}1 \right )\cdot 2+1\]This yields an integer that contains 2589 digits. But now try to add 1 to it, and it yields infinity. So I think that's Prime's largest allowed integer, approximately 3.6E2588. X<> c Joe 

08182017, 03:18 PM
Post: #7




RE: (12C) Combination and Permutation
Nice info there Joe! Many thanks for sharing!
impressive the 50g though, as I would have expected the prime to show off with all its memory. Wikis are great, Contribute :) 

08182017, 04:58 PM
Post: #8




RE: (12C) Combination and Permutation
(08182017 08:55 AM)pier4r Wrote: Just giving another comparison (against the sharp el506w with built in functions) Hi, I see there are a few error entries. You might be interested in this thread in the old forum: http://www.hpmuseum.org/cgisys/cgiwrap/...ead=173300 

08182017, 05:12 PM
(This post was last modified: 08182017 05:13 PM by pier4r.)
Post: #9




RE: (12C) Combination and Permutation
(08182017 04:58 PM)BartDB Wrote: Hi, I see there are a few error entries. You might be interested in this thread in the old forum: Nice find! As users in the thread linked observed I have the 506W not the W506, still the creativity impresses me to abuse the integration function. This are the real calculator users! The ones that know the math/implementation well enough to bent the calculator in many cases to their needs, even when according to the manual there is no other way. Let's see if I am able to reproduce the steps. Wikis are great, Contribute :) 

08192017, 05:26 PM
(This post was last modified: 08192017 06:00 PM by Dieter.)
Post: #10




RE: (12C) Combination and Permutation
(08172017 09:49 PM)BartDB Wrote: The C(n,r) routine is based on the identity C(n,r) = C(n,nr), selecting the minimum value of r and nr 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. (08172017 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 HP12C. Since doing division can result in noninteger 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. (08172017 09:49 PM)BartDB Wrote: The P(n,r) routine is based on the identity P(n,r) = n x (n1) x (n2) x ... x (nr+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 »

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