# HP Forums

Full Version: (12C) Factorial in Stack
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
02 INTG
03 LSTX
04 1
05 -
06 X = 0
07 GTO 10
08 x
09 GTO 03
10 R↓
Example: FIX 0
12 [R/S] 479,001,600
69 [R/S] 1.711224 98

Gamo 4/7/2023
Hi. Why do you do INTG twice?
(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.
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.
3 steps version without n! here:

If you want some user friendly interaction, 9 steps, also included on the link.

Cs.
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.
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?