The Museum of HP Calculators

HP Forum Archive 16

[ Return to Index | Top of Index ]

Permutation, Combination and Factoral for the 29C
Message #1 Posted by Hal on 30 July 2006, 11:40 p.m.

Hi Everybody. Checking my son's homework the other night...statistics...I decided to throw a little program into my 29c that would crank out factoral, permutation and combination. After doing so, it dawned on me that the 29c/19c applications manual probably already has such a program in it, and such is indeed the case. To my (pleasant) surprise, however, the program in the applications manual is twice as long as mine (84 steps vs 42), and doesn't preserve the stack registers(y,z and t) when computing factorals, which mine does. (Note that machines with a hard coded factoral function also preserve y,z and t, safeguarding any intermediate results you may have sitting there.) Computation times are virtually the same as the program in the App manuel, with 69! taking about 27 seconds to compute. Smaller values of course take correspondingly less time, as with any purely iterative routine.
So, for what it's worth, here's the program listing.
Comments, suggestions, criticism...love to hear 'em. Best regards, Hal

Directions:  key in total elements (m), Enter, key in subset (n). 	
	For permutation, gsb 1
	For combinations, gsb 2
	Example: 20 objects total, how many arrangements of 5 can you make…20 enter 5 gsb 1.  Result should be 1,860,480    
	Example: 20 objects total, how many combinations of 5 can you make…20 enter 5 gsb 2.  Result should be 15,504    
	For x! (factoral) key in x, gsb 3
	Example: find 8!…8 gsb 3.  Result should be 40,320. 
	Note:  Factoral subroutine preserves existing values in the y, z, and t registers  
	registers used: 0,5,6,7,8,9
	labels used: 1,2,3,4

	HP 29C program:
1	lbl 2
2	sto 8
3	gsb 1
4	sto 9
5	rcl 8
6	rcl 8
7	gsb 1
8	rcl 9
9	x<>y
10	/
11	rtn
12	lbl 1
13	1
14	-
15	sto 0
16	x<>y
17	sto 7
18	sto 6
19	lbl 4
20	1
21	sto - 6
22	rcl 6
23	sto x 7
24	DSZ
25	gto 4
26	rcl 7
27	rtn
28	lbl 3
29	r dn
30	sto 5
31	r dn
32	sto 8
33	r dn
34	sto 9
35	r dn
36	enter^
37	gsb 1
38	rcl 9
39	scl 8
40	rcl 5
41	rcl 7
42	rtn

      
Re: Permutation, Combination and Factoral for the 29C
Message #2 Posted by Vieira, Luiz C. (Brazil) on 31 July 2006, 12:06 a.m.,
in response to message #1 by Hal

Congrats!

Very good programming example. And a very good program itself, indeed.

Thanks for sharing.

Cheers.

Luiz (Brazil)

Edited: 31 July 2006, 12:07 a.m.

      
Re: Permutation, Combination and Factoral for the 29C
Message #3 Posted by Thomas Okken on 31 July 2006, 8:04 a.m.,
in response to message #1 by Hal

For computing C(m,n), instead of computing P(m,n) and then dividing by n!, it's better to rearrange the computation like this:

   C(m,n) = m / 1 * (m-1) / 2 * (m-2) / 3 ... * (m-n+1) / n

All the intermediate results will still be integers, and you avoid the overflow that would otherwise occur if P(m,n) is too large while C(m,n) is not.

- Thomas


[ Return to Index | Top of Index ]

Go back to the main exhibit hall