Post Reply 
(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
(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
Find all posts by this user
Quote this message in a reply
04-17-2023, 12:22 PM
Post: #2
RE: (12C) Factorial in Stack
Hi. Why do you do INTG twice?
Find all posts by this user
Quote this message in a reply
04-17-2023, 06:34 PM
Post: #3
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
04-18-2023, 06:13 AM
Post: #4
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)
   
Visit this user's website Find all posts by this user
Quote this message in a reply
04-18-2023, 07:01 AM
Post: #5
RE: (12C) Factorial in Stack
3 steps version without n! here:
https://www.hpmuseum.org/forum/thread-10...l#pid90191

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

Cs.
Find all posts by this user
Quote this message in a reply
04-22-2023, 04:36 AM
Post: #6
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2023, 07:32 AM
Post: #7
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?

Perhaps see also (12C Platinum) Internal Precision Test
Find all posts by this user
Quote this message in a reply
04-22-2023, 07:53 AM
Post: #8
RE: (12C) Factorial in Stack
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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