(12C) Factorial in Stack
04-07-2023, 08:10 AM (This post was last modified: 04-07-2023 08:11 AM by Gamo.)
Post: #1
 Gamo Senior Member Posts: 751 Joined: Dec 2016
(12C) Factorial in Stack
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
04-17-2023, 12:22 PM
Post: #2
 Tritonio Junior Member Posts: 5 Joined: Apr 2023
RE: (12C) Factorial in Stack
Hi. Why do you do INTG twice?
04-17-2023, 06:34 PM
Post: #3
 Dave Britten Senior Member Posts: 2,321 Joined: Dec 2013
RE: (12C) Factorial in Stack
(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.
04-18-2023, 06:13 AM
Post: #4
 Steve Simpkin Senior Member Posts: 1,216 Joined: Dec 2013
RE: (12C) Factorial in Stack
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.

Attached File(s) Thumbnail(s)

04-18-2023, 07:01 AM
Post: #5
 Csaba Tizedes Senior Member Posts: 608 Joined: May 2014
RE: (12C) Factorial in Stack
3 steps version without n! here:

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

Cs.
04-22-2023, 04:36 AM
Post: #6
 Steve Simpkin Senior Member Posts: 1,216 Joined: Dec 2013
RE: (12C) Factorial in Stack
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.
04-22-2023, 07:32 AM
Post: #7
 EdS2 Senior Member Posts: 600 Joined: Apr 2014
RE: (12C) Factorial in Stack
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?