Post Reply 
(33C) (33E) C(y,x), P(y,x) and x!
05-07-2018, 06:36 PM (This post was last modified: 05-07-2018 06:44 PM by Rogier.)
Post: #1
(33C) (33E) C(y,x), P(y,x) and x!
The 33E was (is) my first programmable calculator. Limited in capacity compared to most other HP-programmables, but you can still fit quite a lot in 49 steps + jumps and subroutines.
HP supplies solutions programs for C(y,x) and P(y,x), but these are bulky. This program calculates C(y,x), P(y,x) and x! in 43 lines.

USE:
y ENTER x R/S > C(y,x) X<=>Y P(y,x)
0 ENTER x R/S > x!

NOTE:
- x and y must be 0 or positive integers
- x<y
- y-x must not exceed 69
- y can be >69. E.g. the program can calculate C(100,50) and P(100,50). Just try.

Code:

01 X=0?
02 GTO 38
03 Sto 3
04 X <=> Y
05 X=0?
06 GTO 41
07 Sto 2
08 X<=>Y
09 -
10 Sto 4
11 Rcl 2
12 GSB 27
13 Sto 0
14 0
15 Sto 4
16 Rcl 4
17 GSB 27
18 Sto / 0
19 Rcl 2
20 Rcl 4
21 -
22 GSB 27
23 Rcl 0
24 *
25 Rcl 0
26 GTO 00
27 Sto 1
28 1
29 -
30 Rcl 3
31 X=Y?
32 GTO 36
33 X<=>Y
34 Sto * 1
35 GTO 28
36 Rcl 1
37 Rtn
38 1
39 ENTER
40 GTO 00
41 Sto 3
42 X<=>Y
43 GSB 27

Registers used:
0 Total
1 Subtotal
2 Y
3 X
4 Y-X

WORKING:
Line 01-06 sort out the special cases;
- (y,0) [> GTO 38, display 1 and stop] or
- (0,x) [GTO 41, call the x! routine (27) and stop. This may seem unnecessarily complicated but the 33C/E interprets Rtn without preceding GSB as R/S, not as GTO 00 like I want.]
After this the program calculates C(y,x) = y!/[(x!)(y-x)!] then it multiplies C(y,x) by x! to get P(y,x). (I could swap calculation of P(y,x) and C(y,x), but then I can't use y!/x! And that is what enables the program tot function with y>69).
Lines 27 ... 37 form the main loop. This loop is set to terminate on y-x (to speed up calculation of y!/x!), or on 0 (to calculate x!)
Find all posts by this user
Quote this message in a reply
05-07-2018, 08:46 PM (This post was last modified: 05-07-2018 08:47 PM by Dieter.)
Post: #2
RE: (33C) (33E) C(y,x), P(y,x) and x!
(05-07-2018 06:36 PM)Rogier Wrote:  The 33E was (is) my first programmable calculator. Limited in capacity compared to most other HP-programmables, but you can still fit quite a lot in 49 steps + jumps and subroutines.
HP supplies solutions programs for C(y,x) and P(y,x), but these are bulky. This program calculates C(y,x), P(y,x) and x! in 43 lines.

USE:
y ENTER x R/S > C(y,x) X<=>Y P(y,x)
0 ENTER x R/S > x!

Ah, permutations and combinations – a true classic on programmable calculators without these functions. That's why this topic has been discussed here several times. I'd like to invite you to this thread from last year. These are programs for the 12C, a calculator which is even more limited than the 33E/C. For instance it does not feature subroutines.

The first programs posted there required the factorial function. Then an iterative approach similar to yours was implemented, for instance in post#22 (my attempt at this topic). This version does it in 23 steps. There also is a "deluxe version" with some optimizations (e.g. by minimizing the number of loops due to C(n,k) = C(n,n–k).

No, there is no special factorial function, but you can get this as well by entering n ENTER 1–n. For instance 5 ENTER –4 returns 120. #-)

Dieter
Find all posts by this user
Quote this message in a reply
05-09-2018, 06:43 PM (This post was last modified: 05-09-2018 06:54 PM by Rogier.)
Post: #3
RE: (33C) (33E) C(y,x), P(y,x) and x!
I hadn't found that thread yet. You are of course right; a program many owners of programmable calulators wrote at one time or another. Interesting to see how others approached the problem.

Still: a very useful program and the first posted program for 33C/E as far as I can see.
Find all posts by this user
Quote this message in a reply
05-09-2018, 08:35 PM
Post: #4
RE: (33C) (33E) C(y,x), P(y,x) and x!
(05-09-2018 06:43 PM)Rogier Wrote:  I hadn't found that thread yet. You are of course right; a program many owners of programmable calulators wrote at one time or another. Interesting to see how others approached the problem.

Yes, that's the reason why I posted the link. And here's another one that is worth reading. All these discussions contain some useful information, e.g. on how to avoid overflow as long as possible, or how to calculate C(n,k) with minimal accuracy loss due to roundoff errors.

(05-09-2018 06:43 PM)Rogier Wrote:  Still: a very useful program and the first posted program for 33C/E as far as I can see.

I think it sure is the first 33C/E combinations/permutations program on this site, and maybe even the first program at all for this calculator. But the linked 12C programs should also run on the 33C/E. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
05-10-2018, 12:38 PM (This post was last modified: 05-10-2018 12:39 PM by Rogier.)
Post: #5
RE: (33C) (33E) C(y,x), P(y,x) and x!
Quote:But the linked 12C programs should also run on the 33C/E.

Not straight away; the 12C has various functions (e.g. x!), which the 33C/E has not, so you may need some extra lines.

I was never very interested in the business-models like 38C/E and 12C, but their programming system really is close to the 33C/E. Actually more close even to the 25(C), which doesn't have subroutines either.
Find all posts by this user
Quote this message in a reply
05-10-2018, 01:32 PM
Post: #6
RE: (33C) (33E) C(y,x), P(y,x) and x!
(05-10-2018 12:38 PM)Rogier Wrote:  
Quote:But the linked 12C programs should also run on the 33C/E.

Not straight away; the 12C has various functions (e.g. x!), which the 33C/E has not, so you may need some extra lines.

Yes, the first programs in the linked thread require the factorial function. That's why I wrote:

(05-07-2018 08:46 PM)Dieter Wrote:  The first programs posted there required the factorial function. Then an iterative approach similar to yours was implemented, for instance in post#22 (my attempt at this topic). This version does it in 23 steps. There also is a "deluxe version" with some optimizations (e.g. by minimizing the number of loops due to C(n,k) = C(n,n–k).

The two programs in post #22 should also run on the 33C/E.

Dieter
Find all posts by this user
Quote this message in a reply
05-10-2018, 05:48 PM (This post was last modified: 05-10-2018 05:50 PM by Rogier.)
Post: #7
RE: (33C) (33E) C(y,x), P(y,x) and x!
Yes, I know. The similarities are more striking than the differences, and a bit of tinkering is no problem. Besides: if I don't like adapting programs, what am I doing on this forum? As always, part of the fun is in the effort to get a program running. And here the 33C/E (and probably 12C as well) is a real treat. Writing and debugging programs using line-number adressing without facilities for inserting/deleting lines can be time-consuming.

I may even go and check out an HP-12C emulator some day.
Find all posts by this user
Quote this message in a reply
Post Reply 




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