Post Reply 
HP 12C Permutation and Combination
08-15-2017, 04:54 AM
Post: #1
HP 12C Permutation and Combination
I always wish that HP 12C include Permutation and Combination functions since this functions is a must have feature for a Financial Calculator. So I make a very simple program that combine both together. To use it just start at GTO 00 for Permutation and GTO 10 for Combination. Very easy to remember to start at GTO 10 for Combination. Simple and useful and here is the program.


Attached File(s)
.pdf  PermuCom12C.pdf (Size: 93.55 KB / Downloads: 69)
Find all posts by this user
Quote this message in a reply
08-16-2017, 09:12 PM
Post: #2
RE: HP 12C Permutation and Combination
(08-15-2017 04:54 AM)Gamo Wrote:  To use it just start at GTO 00 for Permutation and GTO 10 for Combination. Very easy to remember to start at GTO 10 for Combination.

Here is a version that implements an idea already posted in an earlier thread: have a common entry point (ideally line 00) and use a code number for different functions. Since C(n,k) simply is P(n,k) / k! this can be done in one common routine:

Code:
01 STO 0
02 X=0?
03 e^x
04 STO/0
05 ENTER
06 -
07 +
08 STO*0
09 -
10 n!
11 x<>y
12 n!
13 x<>y
14 /
15 RCL 0
16 n!
17 /
18 GTO 00

Usage:
n [ENTER] k [ENTER] 0 [R/S] => P(n,k)
n [ENTER] k [ENTER] 1 [R/S] => C(n,k)

Instead of 1 any other value ≠ 0 may be used.

Example:
25 [ENTER] 6 [ENTER] 0 [R/S] => 127512000
25 [ENTER] 6 [ENTER] 1 [R/S] => 177000

The first four lines store either 0 or 1 in R0, line 08 turns R0 into 0 or k, and after P(n,k) has been calculated it is divided by R0! (i.e. by 1 or by k!) to obtain the final result. The calculation of P(n,k) is done on the stack and requires no additional register.

Dieter
Find all posts by this user
Quote this message in a reply
08-17-2017, 04:53 AM
Post: #3
RE: HP 12C Permutation and Combination
Thanks Dieter !!!

Your code example of Permutation and Combination is excellent with user friendly way to just input 0 R/S or 1 R/S and the program is much smaller in steps too.

How about a Pricing Program that calculate the Price, Cost, Margin, and Mark Up
Since the HP 12C didn't have this feature and is a must have feature for a Financial Calculator. When you know the two variables and it will calculate the other two unknown variables. I did this on the Solve App on HP Prime and it work perfectly but I always wish I have this pricing feature for HP 12C

Thank You

Gamo
Find all posts by this user
Quote this message in a reply
08-17-2017, 05:16 AM
Post: #4
RE: HP 12C Permutation and Combination
Hello Dieter

I try your Permutation and Combination program and found one little problem with the Permutation answer. Most P answer correctly but with this 25P7 I check most calculator result to 2,422,728,000 but on this program result to 2,442,727999 I'm not sure if this result is more accurate or others just round the result up.

Thank You

Gamo
Find all posts by this user
Quote this message in a reply
08-17-2017, 06:35 AM (This post was last modified: 08-17-2017 07:54 AM by Dieter.)
Post: #5
RE: HP 12C Permutation and Combination
(08-17-2017 05:16 AM)Gamo Wrote:  I try your Permutation and Combination program and found one little problem with the Permutation answer. Most P answer correctly but with this 25P7 I check most calculator result to 2,422,728,000 but on this program result to 2,442,727999

The returned result is 2 442 727 999 and it is not exact.

(08-17-2017 05:16 AM)Gamo Wrote:  I'm not sure if this result is more accurate or others just round the result up.

What you see is the result of limited precision and roundoff. The exact answer is 25!/18! = 15511210043330985984000000/6402373705728000 = 2422728000. But the 12C only has 10 digits, so it actually calculates 1,551121004 E+25 / 6,402373706 E+15 = 2 442 727 999,376... which is what you see here. The numerator is slightly low (rounded down) and the denominator is slightly high (rounded up) which causes a slightly low result. This will happen with any 10-digit calculator if the result is calculated by dividing factorials. You could also do it by evaluating 19 · 20 · 21 · 22 · 23 · 24 · 25 which yields the exact result. Well, at least this time. ;-)

That's why most HPs, including the 12C, internally use 3 more digits for their calculations: 1,551121004333 E+25 / 6,402373705728E+15 = 2422727999,9998... which rounds to 2422728000 in the returned 10-digit result. OK, actually these calculators will evaluate permutations by 19 · 20 · ...  · 25 which is faster and causes less roundoff problems. ;-)

For more information on this topic and ways to calculate permutations and combinations with minimized roundoff errors cf. this thread, especially Werner's post #4. Doing this without factorials also allows larger arguments, i.e. no overflows unless the result overflows. But all this is quite cumbersome on the 12C which does not feature any loop counters.

Edit: anyway, here is a short permutation routine that shows how it can be done.

Code:
01 CHS
02 x<>y
03 +
04 1
05 STO 0
06 R↓
07 LstX
08 x≤y?
09 GTO 14
10 STO*0
11 1
12 -
13 GTO 08
14 RCL 0
15 GTO 00

25 [ENTER]  7  [R/S] => 2422728000
99 [ENTER] 33 [R/S] => 1,714468 E+63

Dieter
Find all posts by this user
Quote this message in a reply
08-17-2017, 10:01 AM
Post: #6
RE: HP 12C Permutation and Combination
Hello Dieter

Very straightforward explanation of the 10 digits calculation limit for the HP 12C
I try the same 25P7 on the HP 15C build-in functions and the result is
2 442 728 000

I though that HP 12C and HP 15C is the same family and should be limited to the same 10 digits calculation limit or the HP 15C use difference formula to get the result?

Gamo
Find all posts by this user
Quote this message in a reply
08-17-2017, 11:34 AM
Post: #7
RE: HP 12C Permutation and Combination
Hello Dieter

When calculate 25P7 manually the result is right at 2 422 728 000

Manual Calculation steps

25 ENTER 7 - [n!] 25 [n!] / [1/x]


Gamo
Find all posts by this user
Quote this message in a reply
08-17-2017, 12:03 PM
Post: #8
RE: HP 12C Permutation and Combination
(08-17-2017 11:34 AM)Gamo Wrote:  When calculate 25P7 manually the result is right at 2 422 728 000
Manual Calculation steps
25 ENTER 7 - [n!] 25 [n!] / [1/x]

Why do you do the division the other way round and then invert it with [1/x] ?

25 [n!] 25 [ENTER] 7 – [n!] [/]    ...and you'll see the same deviation again.

There is always the chance that roundoff errors may occur or not, or they may be larger or lesser, depending on the way you do your calculation and, more important, on the numeric values of a particular case:

Try P(25, 8) and get slightly low 43609103990 with your method and the correct 43609104000 with the other one.

10 digits is all you got on the 12C. This means that after several operations the final result may not have 10 exact digits.

Dieter
Find all posts by this user
Quote this message in a reply
08-17-2017, 12:25 PM (This post was last modified: 08-17-2017 12:27 PM by Gamo.)
Post: #9
RE: HP 12C Permutation and Combination
Hello Dieter

Formula: mPn = m! / (m-n)!

I decided to calculate manually this way because of the rule of operation that must start from the inner most parentheses first and I have no choice to reverse the division by using 1/x since the problem start from the denominator portion of the fraction.
I believe that calculate this way give more accurate result.

Gamo
Find all posts by this user
Quote this message in a reply
08-17-2017, 06:12 PM
Post: #10
RE: HP 12C Permutation and Combination
(08-17-2017 10:01 AM)Gamo Wrote:  I try the same 25P7 on the HP 15C build-in functions and the result is
2 442 728 000

I though that HP 12C and HP 15C is the same family and should be limited to the same 10 digits calculation limit or the HP 15C use difference formula to get the result?

Here is what I wrote:

Quote:That's why most HPs, including the 12C, internally use 3 more digits for their calculations

Both the 12C and the 15C internally (!) use 13 digits so that the final 10-digit result that is presented to the user is correct (ok, mostly). So the 15C's internal nCr and nPr functions are performed with 13 digits. This usually (but not always) gives results with at least 10 exact digits – which is what you see in the display. On the other hand user programs like the ones we're discussing here always work with only 10 digits. This can cause roundoff errors. For instance, calculating 1/3 * 3 with the usual 10 digits yields 0,99999 99999 while with 13 digits you get 0,99999 99999 999 which, after rounding to 10 display digits, shows up as "1".

Internally the 15C probably calculates permutations and combinations by repeated multiplication, similar to the method in the thread I linked to. We can safely assume that it does not call the factorial routine. This means that C(25, 7) is almost certainly not calculated by first evaluating 1 · 2 · 3 · ... · 25, then 1 · 2 · 3 · ... · 18 and finally 1 · 2 · 3 · ... · 7 and then determining the permutations from these three. Take a look at the linked thread and you get an impression of how it can (and should) be done: 25 / 1 · 24 / 2 · 23 / 3 · 22 / 4 · 21 / 5 · 20 / 6 · 19 / 7 = 480700. With this method all intermediate results are integers so that roundoff errors are minimized.

Dieter
Find all posts by this user
Quote this message in a reply
08-17-2017, 06:32 PM (This post was last modified: 08-17-2017 06:33 PM by Dieter.)
Post: #11
RE: HP 12C Permutation and Combination
(08-17-2017 12:25 PM)Gamo Wrote:  I decided to calculate manually this way because of the rule of operation that must start from the inner most parentheses first

First of all, you do not have to start at the innermost parentheses.

(08-17-2017 12:25 PM)Gamo Wrote:  and I have no choice to reverse the division by using 1/x since the problem start from the denominator portion of the fraction.

There always is a choice in RPN. Instead of first dividing "the wrong way round" and then correcting this by [1/x] you can simply use [x<>y]. If you really want to start with the denominator (which does not make much sense here), and after the numerator 25! has been calculated afterwards, simply press [x<>y] [/]. This way numerator and denominator are swapped.

(08-17-2017 12:25 PM)Gamo Wrote:  I believe that calculate this way give more accurate result.

I do not think so. ;-)

Not convinced? Just try a very simple example like P(7, 1). Or P(13, 8). Or P(11, 8). Or... Whether the one or the other method causes more or less roundoff errors depends on the two arguments and how well the intermediate results happen to round to 10 digits. Just the way it is with most finite precision calculations.

Dieter
Find all posts by this user
Quote this message in a reply
08-17-2017, 09:57 PM (This post was last modified: 08-17-2017 10:07 PM by BartDB.)
Post: #12
RE: HP 12C Permutation and Combination
Hi,

Your routines are likely to fail due to overflow if n is greater than 69, even if the final result is less than 1E100.

I have added to the Software Library routines for the 12C for Combination and Permutation that should work for most instances where the result is less than 1E100.

http://www.hpmuseum.org/forum/thread-8856.html

Best regards.


Visit this user's website Find all posts by this user
Quote this message in a reply
08-17-2017, 10:03 PM
Post: #13
RE: HP 12C Permutation and Combination
(08-17-2017 10:01 AM)Gamo Wrote:  I try the same 25P7 on the HP 15C build-in functions and the result is
2 442 728 000

Gamo

Interesting, on my original HP-15C (not LE), for 25P7 using built-in function I get the result
2 422 728 000


Visit this user's website Find all posts by this user
Quote this message in a reply
08-18-2017, 08:11 AM
Post: #14
RE: HP 12C Permutation and Combination
Try 25P7 this time on HP Prime
Home Mode
PERM(25, 7) result 2 422 728 000
Put in the formula manually
25! / (25-7)! = 2 422 727 999.99

CAS Mode both ways return 2 422 728 000


Gamo
Find all posts by this user
Quote this message in a reply
08-18-2017, 01:05 PM
Post: #15
RE: HP 12C Permutation and Combination
(08-17-2017 04:53 AM)Gamo Wrote:  How about a Pricing Program that calculate the Price, Cost, Margin, and Mark Up
Since the HP 12C didn't have this feature and is a must have feature for a Financial Calculator. When you know the two variables and it will calculate the other two unknown variables. I did this on the Solve App on HP Prime and it work perfectly but I always wish I have this pricing feature for HP 12C

Thank You

Gamo

Hi Gamo,

For most of these problems it's simple enough just to use the various % functions to calculate what you need. Maybe the only difficulty is remembering how to convert a know margin to a markup or vice-versa, or at least doing it in an efficient way. It's late at night and I don't have my calculator on me but I imagine using Dieters idea of x=0? to separate the two would be the best way to go about it. The keystrokes needed are- If margin is known: e.g. 30- 30 enter enter 1 x<>y % - /, if markup is know then it's the same but replace the - with a +

I'm starting to formulate in my mind a way that you could store the known variables and then have the unknowns stored e.g. in 1 for cost, 2 for price, 3 for markup, 4 for margin. I haven't had much experience programming the 12c aside from automating simple repetitive tasks so I'll have to spend some time thinking about it, maybe in the morning if I've got some time. Mostly inspired to do it a learning exercise.

Occasionally I'll use one of my other financial calculators that have menu based functions and think how it can make things simpler or more intuitive, but I like that the 12c makes me understand a bit more what is going on in calculations like this. And this is one of those cases where it's faster to just do the manual calculation- and in many cases doing it manually will actually give you more information e.g. if you know your margin % and your price, what is cost, and what is the margin in $ terms i.e. gross profit. The menu based system will leave you to calculate that GP figure, but using RPN and doing it manually you will come up with the figure naturally.

One area I do really appreciate a menu driven system with labels is in amortization schedules, but for the most part I'm finding the 12c just makes sense, and it feels faster.


Regards,

Zac
Find all posts by this user
Quote this message in a reply
08-18-2017, 07:25 PM (This post was last modified: 08-18-2017 08:21 PM by Dieter.)
Post: #16
RE: HP 12C Permutation and Combination
(08-17-2017 09:57 PM)BartDB Wrote:  Your routines are likely to fail due to overflow if n is greater than 69, even if the final result is less than 1E100.

Sure. They use the factorial function which overflows at x=70.

(08-17-2017 09:57 PM)BartDB Wrote:  I have added to the Software Library routines for the 12C for Combination and Permutation that should work for most instances where the result is less than 1E100.

http://www.hpmuseum.org/forum/thread-8856.html

Ah, yes. This program seems to use a technique similar to that in the thread I linked to, i.e. it does the calculation by repeated multiplication.

However, this can be improved. ;-) You say that the result is rounded to the nearest integer (to correct roundoff errors). But as far as I can see the result is not rounded but truncated. So if the result happens to be, say, 6,999999999 it is returned as 6 and not 7. If you want to round you should add a small amount like 0,1 before the INTG.

As already said earlier, the roundoff problem in the nCr routine can be avoided (well, almost) if the divisor is counted up instead of down. This way the intermediate results are exact integers. The following program uses this technique. And it also distinguishes nPr and nCr by the sign of r: if r<0 then nCr is calculated, else nPr. If r=0 the result is 1 in both cases. ;-)

Code:
01 ENTER
02 CLX
03 x≤y?
04 GTO 31
05 R↓
06 CHS
07 x<>y
08 STO 2
09 x<>y
10 -
11 LstX
12 x≤y?
13 x<>y
14 x<>y
15 STO 0
16 1
17 STO 1
18 STO 3
19 RCL 0
20 x=0?
21 GTO 46
22 RCL 2
23 STO*3
24 RCL 1
25 STO/3
26 1
27 STO-0
28 STO+1
29 STO-2
30 GTO 19
31 R↓
32 STO 0
33 x<>y
34 STO 2
35 1
36 STO 3
37 RCL 0
38 x=0?
39 GTO 46
40 RCL 2
41 STO*3
42 1
43 STO-0
44 STO-2
45 GTO 37
46 RCL 3
47 GTO 00

Example:
25 [ENTER]  7  [R/S] => 2 422 728 000
25 [ENTER] –7 [R/S] => 480 700

BTW, the above program also gets 90 C 7, 101 C 6 and 70 C 8 right.
On the other hand it overflows at 325 C 167. But there is a way... cf. the thread I linked to earlier.
And I like your short nPr routine – maybe we should combine both versions. ;-)

Edit: I tried just that and used your nPr routine. This way the whole program would be down to 44 lines.
But it seems that nP0 is not calculated correctly, the result is n instead of 1. Could you please check that?

Dieter
Find all posts by this user
Quote this message in a reply
08-19-2017, 01:40 AM
Post: #17
RE: HP 12C Permutation and Combination
Hi Zac,

Thank You for thinking about the Pricing Calculation program since I don't see anyone try this at all. Even on the HP 12C User Manual Solution Handbook got short program on page 93 but to execute the program is kind of confusing. So I did try this on the HP Prime which is a lot easier to use just by input two known variables. I used the Solve App and put two equations related to each other with variables P-Price C-Cost A-Margin and U-Markup with this equations you can quickly see the result.

   
   


Gamo
Find all posts by this user
Quote this message in a reply
08-20-2017, 09:37 AM
Post: #18
RE: HP 12C Permutation and Combination
(08-19-2017 01:40 AM)Gamo Wrote:  Hi Zac,

Thank You for thinking about the Pricing Calculation program...

Hi Gamo, I've had a go at creating a program to do cost/price/markup/margin.

I'm going to post it in a new thread so that I can get feedback on my program and ideas on how to make it briefer, if possible. I'm pretty sure that I've repeated steps in certain places where I could have branched. It's good for what it was supposed to be; a learning opportunity.


Regards,

Zac
Find all posts by this user
Quote this message in a reply
08-23-2017, 06:40 AM
Post: #19
RE: HP 12C Permutation and Combination
Hello Dieter

Your program is very nice and easy to use so I use it a lot and find out another problem with 60p5 this time the result show 655 381 440.2 on regular cheap Casio fx-260 solar gave result 655 381 440

Is it possible just to add INTG to the program?

Thank You

Gamo
Find all posts by this user
Quote this message in a reply
08-23-2017, 03:00 PM
Post: #20
RE: HP 12C Permutation and Combination
(08-18-2017 08:11 AM)Gamo Wrote:  Try 25P7 this time on HP Prime
Home Mode
PERM(25, 7) result 2 422 728 000
Put in the formula manually
25! / (25-7)! = 2 422 727 999.99

CAS Mode both ways return 2 422 728 000


Gamo

BTW, Droid48sx app on android smartphone gives the same results:
25 [enter] 7 PERM = 2422728000
25! / (25-7)! = 2422727999.99
Find all posts by this user
Quote this message in a reply
Post Reply 




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