(12C) Factorial in Stack - 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) Factorial in Stack ( /thread-19782.html) |

(12C) Factorial in Stack - Gamo - 04-07-2023 08:10 AM
Been a long time, anyway here is a little 10 steps program to calculate Factorial using only the stack in program. The computation speed is really good but can not compare with the [n!] function. Program Quote:01 INTG // Additional INTG corrects fractions.Example: FIX 0 12 [R/S] 479,001,600 69 [R/S] 1.711224 98 Gamo 4/7/2023 RE: (12C) Factorial in Stack - Tritonio - 04-17-2023 12:22 PM
Hi. Why do you do INTG twice? RE: (12C) Factorial in Stack - Dave Britten - 04-17-2023 06:34 PM
(04-17-2023 12:22 PM)Tritonio Wrote: Hi. Why do you do INTG twice? I think it's to make sure that the LSTX in step 3 also contains an integer value. RE: (12C) Factorial in Stack - Steve Simpkin - 04-18-2023 06:13 AM
This program works nicely on the HP-25 too. It is the same number of steps as the Factorial program included in the HP-25 Applications Programs book but that uses Reg 0 and will return incorrect results if the input is not an integer. RE: (12C) Factorial in Stack - Csaba Tizedes - 04-18-2023 07:01 AM
3 steps version without n! here: https://www.hpmuseum.org/forum/thread-10059-post-90191.html#pid90191 If you want some user friendly interaction, 9 steps, also included on the link. Cs. RE: (12C) Factorial in Stack - Steve Simpkin - 04-22-2023 04:36 AM
Yes, the built-in n! function is much quicker but may not be as accurate. The OP’s program takes 28 seconds to calculate 69! on my 1987 USA HP-12C. The built-in n! function takes less than a second. When I subtract the results I get a difference of 2.E89. I am not 100% sure which result is more correct. I would expect the brute force program listed here would be more accurate since it only employs multiplication but the result is beyond 10 digits in this case so rounding is occurring. RE: (12C) Factorial in Stack - EdS2 - 04-22-2023 07:32 AM
The value of 69! in full is 171 122 452 428 141 311 372 468 338 881 272 839 092 270 544 893 520 369 393 648 040 923 257 279 754 140 647 424 000 000 000 000 000 Can you tell which is closer? Perhaps see also (12C Platinum) Internal Precision Test RE: (12C) Factorial in Stack - Steve Simpkin - 04-22-2023 07:53 AM
EdS2, The built-in n! function is closer. It’s final result is accurate to 10-digits (1711224524). The program returns 1711224522 so it is only accurate to 9-digits. Rounding off to 10 significant digits after each multiplication and using that for the next multiplication takes its toll. |