Post Reply 
(12C) Combination and Permutation
08-17-2017, 09:49 PM (This post was last modified: 08-17-2017 10:05 PM by BartDB.)
Post: #1
(12C) Combination and Permutation
C(n,r) and P(n,r) for HP-12C.

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,n-r), selecting the minimum value of r and n-r 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 HP-12C. Since doing division can result in non-integer 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 (n-1) x (n-2) x ... x (n-r+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=n-r
02  lastx   ; X=r, Y=n-r
03  X<>y    ; for X and Y comparison in next step
04  x<=y    ; would like to use X>Y? test to save previous step, but not available on 12C
05  x<>y    ; place minimum of r and n-r in X 
06  +       ; X=n
07  sto 1   ; Store n in R1
08  lastx   ; X=min(r,n-r)
09  sto 0   ; store in R0. Use as loop counter
10  1       ; Initial value is 1
11  sto 3   ; R3 = result register
12  rcl 0   ; X = loop counter
13  x=0     ; have we reached the end?
14  gto 22  ; if so, then exit
15  sto/ 3  ; update the result
16  rcl 1   ; recall next numerator factor
17  sto* 3  ; update the result
18  1       ;
19  sto- 0  ; decrement loop count
20  sto- 1  ; decrement numerator factor
21  gto 12  ; loop
22  rcl 3   ; place the result on the stack
23  intg    ; take integer 
24  gto 00  ; end

25  x<>y    ; P(n,r) routine. X=n, Y=r 
26  sto 0   ; Store n in R0 
27  x<>y    ; X=r, Y=n 
28  -       ; X=n-r
29  rcl 0   ; X=n, y=n-r 
30  1       ;
31  -       ; subtract 1 from X 
32  x<=y    ; are we done yet? 
33  gto 36  ; if so, then display the result 
34  sto* 0  ; update result 
35  gto 30  ; loop 
36  rcl 0   ; display the result
37  gto 00  ; end

Accuracies (wrong didgits are underlined)
Combinations
\begin{array}{|l|r|r|}
\hline Inputs & HP-12C & 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}{|l|r|r|}
\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}


Visit this user's website 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



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