HP Forums
(12C) Permutation & Combination revisit - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Permutation & Combination revisit (/thread-22079.html)



(12C) Permutation & Combination revisit - Gamo - 07-25-2024 08:36 AM

This program calculate the Permutation nPr and Combination nCr

using the HP-12C with this program it can be use to do arithmetic calculation.

-----------------------------------------------------------------------

Usage:

For Permutaion make sure program is at the beginning [f] PRGM

for Combination start program at line 16 [g] GTO 16

nPr is Permutation input n [ENTER] r [R/S] display answer

nCr is Combination input n [ENTER] r [R/S] display answer

Both for n > r

------------------------------------------------------------

Example: FIX 0

11P6 - 8P5 = ?

[f] PRGM

11 [ENTER] 6 [R/S] display 332640

8 [ENTER] 5 [R/S] display 6720 [-] display answer 325920

-----------------------------------------------------------------

12C5 - 9P7 = ?

[g] GTO 16

12 [ENTER] 5 [R/S] display 792

[f] PRGM

9 [ENTER] 7 [R/S] display 181440 [-] display answer -180648

------------------------------------------------------------------

8P3 + 6C3 - 5P3 = ?

[f] PRGM

8 [ENTER] 3 [R/S] display 336

[g] GTO 16

6 [ENTER] 3 [R/S] display 20 [+] display 356

[f] PRGM

5 [ENTER] 3 [R/S] display 60 [-] display answer 296

---------------------------------------------------------
Program:
Quote:01 X<>Y
02 n!
03 LSTx
04 X<>Y
05 R↓
06 X<>Y
07 -
08 n!
09 X<>Y
10 R↓
11 ÷
12 X<>Y
13 R↓
14 GTO 00
15 R/S
16 X<>Y
17 n!
18 LSTx
19 X<>Y
20 R↓
21 X<>Y
22 -
23 LSTx
24 n!
25 X<>Y
26 n!
27 x
28 X<>Y
29 R↓
30 ÷
31 X<>Y
32 R↓
33 GTO 15

Gamo 7/2024


RE: (12C) Permutation & Combination revisit - Thomas Klemm - 07-28-2024 10:57 AM

The same, just a little shorter:
Code:
01-       34 : x<>y
02-    43  3 : n!
03-       34 : x<>y
04-    43 36 : LSTx
05-       34 : x<>y
06-       30 : -
07-    43  3 : n!
08-       10 : ÷
09- 43,33 00 : GTO 00
10-       31 : R/S
11-       34 : x<>y
12-       36 : ENTER
13-    43  3 : n!
14-       33 : R↓
15-       34 : x<>y
16-       30 : -
17-    43 36 : LSTx
18-    43  3 : n!
19-       34 : x<>y
20-    43  3 : n!
21-       20 : ×
22-       34 : x<>y
23-       33 : R↓
24-       10 : ÷
25-       34 : x<>y
26-       33 : R↓
27- 43,33 10 : GTO 10

Now you have to GTO 11 to calculate Combination nCr.


RE: (12C) Permutation & Combination revisit - C.Ret - 07-28-2024 04:29 PM

Dear friends,

These codes are interesting, but they are actually two codes in one.

If we are allowed to use a memory register and without changing the way we enter \(n\) and \(k\) in the stack, it is possible to make a shorter code.

Indeed \( P(n,k) = \frac{n!}{(n-k)!}\) and \(C(n,k)=\frac{n!}{(n-k)! \cdot k!}\) thus \(C(n,k)=\frac{P(n,k)}{k!}\).

The idea is that instead of typing a GTO xx to start the calculation of the combinations from scratch, it is enough to press R/S when the number of Permutations \(P(n,k)\) is displayed to continue and terminate the computation of the Combinaisons \(C(n,k)\).

So I propose the following code:

Code:
01-    44 0  STO 0
02-      33  R↓
03-   43  3  n!
04-   43 36  LSTx
05-    45 0  RCL 0
06-      30   -
07-   43  3  n!
08-      10   ÷
09-      31  R/S
10-    45 0  RCL 0
11-43,33 07  GTO 07

P.S.: It is possible to save an instruction by changing the way \(n\) and \(k\) are entered in the stack (but this make a lot of R/S - this version is the easier to use.

Usage:
Permutations:
Restart from start : [ f ] [ Prgm ]
Enter \(n\) and \(k\) : n [ENTER] k
Press [R/S] to run the code and get the # of permutations \(P(n,k)\)

Combinaisons:
Restart from start : [ f ] [ Prgm ]
Enter \(n\) and \(k\) : n [ENTER] k
Press [R/S] to run the code and press [R/S] a second time when Permutations are displayed to get the # of Combinaisons \(C(n,k)\).

Example:
8P3 + 6C3 - 5P3 = ?

8 [ f ][ PRGM ] 3 [R/S] display 336
6 [ f ][ PRGM ] 3 [R/S] display 120 [R/S] display 20 [+] display 356
5 [ f ][ PRGM ] 3 [R/S] display 60 [-] display answer 296.


EDIT: 24-AOUT-07 Typo in key-codes of RCL 0. Mnemonic Rdn replace by R↓ see folling posts.


RE: (12C) Permutation & Combination revisit - Matt Agajanian - 07-28-2024 05:57 PM

Excellent code. Most efficient. Thanks!


RE: (12C) Permutation & Combination revisit - AnnoyedOne - 08-04-2024 03:09 PM

IMHO "Rdn" probably should be "ROLL". However the keycode (33) is correct.

The "RCL 0" keycodes should be "45 0" not "44 0".

The code works in my HP HP-12C PC (v1.2 11/29/2012) & Android (v1.6.5 5/26/2015) "virtual calculators". I don't have a HP-12C.


RE: (12C) Permutation & Combination revisit - C.Ret - 08-07-2024 05:32 PM

(08-04-2024 03:09 PM)AnnoyedOne Wrote:  IMHO "Rdn" probably should be "ROLL". However the keycode (33) is correct.
The "RCL 0" keycodes should be "45 0" not "44 0".

Thanks you to point out this two inaccuracies.
I correct the key-codes of the RCL operations in my post.

Rdn stand for Roll-Down, I may perhaps have use R↓ instead?


RE: (12C) Permutation & Combination revisit - AnnoyedOne - 08-07-2024 05:49 PM

(08-07-2024 05:32 PM)C.Ret Wrote:  I may perhaps have use R↓ instead?
No worries. I didn't know that the forum supported that (hence ROLL). I know my PC text editors don't.