HP 12C Permutation and Combination

08232017, 03:34 PM
Post: #21




RE: HP 12C Permutation and Combination
(08172017 12:25 PM)Gamo Wrote: Hello Dieter Gamo, here's a hint: LSTx number m n! LSTx number n  n! / No need to use decimals at all that way. 

08232017, 06:32 PM
(This post was last modified: 08232017 07:19 PM by Dieter.)
Post: #22




RE: HP 12C Permutation and Combination
(08232017 06:40 AM)Gamo Wrote: 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 fx260 solar gave result 655 381 440 Sure. As already explained (please read my previous posts again) this can and will happen if you calculate with factorials which cannot be represented exactly with 10 digits. (08232017 06:40 AM)Gamo Wrote: Is it possible just to add INTG to the program? That's not a good idea. The error can be both positive and negative. If the result is a bit too small, INTG will be off by one (or even more). Just remember 25P7 on the Prime or any other correctly working 12digit calculator (2422727999,99) resp. on the 10digit 12C (2422727999). You could round the result by adding 0,5 first and then use INTG, but this does not mean that the result is exact – the error may be > 1. If you want to avoid or at least minimize this kind of error you have to do the calculation without factorials. I have described a way to do so and you'll also find an explanation why the preferable method avoids roundoff errors. Here are two programs that will do this job. First, here is a compact version that returns both nCr in X and nPr in Y. If you want to have it the other way round, simply swap the final RCL 3 and RCL 4. Code: 01 STO 0 25 [ENTER] 7 [R/S] => 480700 [x<>y] => 2422728000 This program should (usually) return accurate results if the latter can be respresented exactly with 10 digits. Second, here is a deluxe version that calculates nCr and nPr. A negative r yields nPr, a positive r gives nCr: 25 [ENTER] 7 [R/S] => 480700 25 [ENTER] 7 [R/S] => 2422728000 Code: 01 ENTER Ths program uses a trick (as proposed in the other thread I linked to earlier) to calculate nCr without overflow as long as the result does not exceed 9,999999999 E+99. Of course this also applies to nPr. There is no overflow unless the result overflows. Both programs will correctly return 60 P 5 = 655381440. This is because they do not work with factorials but with repeated multiplication. Just as probably all (or at least most) calculators do in their internal nPr and nCr functions. Of course this method is slower than calculating with factorials. But on a 12CP, let alone an emulator, this should not be an issue. Dieter 

08242017, 07:58 AM
(This post was last modified: 08242017 07:59 AM by Gamo.)
Post: #23




RE: HP 12C Permutation and Combination
I just figure this out about result of Permutation or Combination that sometime return with decimal answer. Just by using FIX 0 and that should solve the problem.
Gamo. 

08242017, 08:03 AM
(This post was last modified: 08242017 08:03 AM by Dieter.)
Post: #24




RE: HP 12C Permutation and Combination
(08242017 07:58 AM)Gamo Wrote: I just figure this out about result of Permutation or Combination that sometime return with decimal answer. Just by using FIX 0 and that should solve the problem. That's the same as rounding to the next integer. There is no guarantee that the result then is fine as the error can exceed 0,5. Gamo, you really need to understand the math behind all this: why these roundoff errors happen and what can be done about it. Please, carefully read again the previous posts in this thread. And use one of the programs I posted yesterday. Dieter 

08242017, 08:11 AM
Post: #25




RE: HP 12C Permutation and Combination
Hello Dieter
deluxe version fix the problem Gamo 

08242017, 10:29 AM
(This post was last modified: 08242017 10:33 AM by Dieter.)
Post: #26




RE: HP 12C Permutation and Combination
(08242017 08:11 AM)Gamo Wrote: deluxe version fix the problem The compact version does so either. Re. your suggestion of setting the display to FIX 0, i.e. rounding the result to the nearest integer: as already mentioned, this does not guarantee a correct result as the error may be as large as one unit in the 9th digit. But if factorials are used the domain for n and r is limited to 0...69 so there there is only a limited number of possible combinations. A test with n=1...69 and r=1...n showed that the worst case is P(64, 5) which is returned as 914941440,4 when calculated with factorials. So rounding to the nearest integer may actually work ...for results < 1E+9. However, there may be errors in the last digit where rounding to the nearest integer does not work as the result already is a 10digit integer. Take the example P(25, 7). It is returned as 25!/18! = 2422727999. The same is true for P(29, 7), for P(41, 6) etc. etc. Rounding to the nearest integer will not change anything as the result already is an integer. #) If you do the rounding by means of the usual procedure "0,5 + INTG" this will increase a 10digit integer by 1 (thus returning a correct result for P(25, 7)), but already accurate 10digit results will be increased as well, which again leads to an error. Results which are 1 unit high will even increase their error. For instance P(42, 6) yields 42!/36! = 3776965921 instead of ...920. Numerical rounding would change this to ...922. Or P(45, 6) is calculated as 45!/39! = 5864443202 which is even 2 ULP high. So rounding, either by setting FIX 0 or numerically, does not fix the problem. Use one of the recent two programs and at least up to 1E+10 the results should be fine. ;) Dieter 

08242017, 01:27 PM
Post: #27




RE: HP 12C Permutation and Combination
Hello Dieter
Both program is much refine they both work very well just like the result that on the buildin functions like you said most calculator must be using this formula. The formula you use I still need to look over and study in more detail and the programing style too. Thank You Gamo 

12042017, 01:30 AM
(This post was last modified: 12042017 03:04 AM by Gamo.)
Post: #28




RE: HP 12C Permutation and Combination
Permutation Manual Calculation yield different answer.
25P7 Manual Calculation steps: 1. 25 ENTER 7  [n!] 25 [n!] / [1/x] result 2,422,728,000 2. 25 [n!] [LSTx] 7  [n!] /  result 2,422,727,999 HP 12C Platinum gave same result either ways Gamo 

« Next Oldest  Next Newest »

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