Example Program to calculate Factorial

01312018, 12:22 PM
(This post was last modified: 01312018 12:23 PM by Gamo.)
Post: #1




Example Program to calculate Factorial
I adapted this "Factorial Program" example from HP25 Owner's Handbook and according to the hand book that let you see how this program work by your own.
I have changed the program to work for HP11C or any other HP RPN programmable calculator that have conditional test and labels. This intended to show the program steps as simple as possible to understand. This example attached as PDF file. Gamo 

01312018, 02:59 PM
Post: #2




RE: Example Program to calculate Factorial
Great! This is also the Example used by Wikipedia for the HP16C. That example has only nine steps, though it does not employ the validity check at the start, and the 16C has the DSZ (decrement, skip if zero) instruction that the 11C does not. It should be possible to make something out of the DSE instruction though. still i like about your program that it is easier to overlook which is good for starters, as the wikipedia example is heavily optimized and not as easy to understand on first look.
Maybe one could extend the Example on to "how to save space" or similar if the second program was added. this is it: Code: 001 LBL F 43,22, F Define label F (mnemonic for "factorial") 

01312018, 06:42 PM
(This post was last modified: 01312018 06:44 PM by Gene.)
Post: #3




RE: Example Program to calculate Factorial  
01312018, 07:50 PM
Post: #4




RE: Example Program to calculate Factorial
(01312018 06:42 PM)Gene Wrote:(01312018 02:59 PM)damaltor Wrote: ...the 16C has the DSZ (decrement, skip if zero) instruction that the 11C does not. DSE works as well as DSZ in this case as the fractional part of the counter is zero ! Just replace DSZ by DSE at step 7, and LBL F by e.g. LBL A at step 1 and the 16C program above works perfectly on the 11C. 

01312018, 07:50 PM
Post: #5




RE: Example Program to calculate Factorial  
01312018, 08:01 PM
(This post was last modified: 01312018 08:06 PM by Dieter.)
Post: #6




RE: Example Program to calculate Factorial
(01312018 02:59 PM)damaltor Wrote: ...the 16C has the DSZ (decrement, skip if zero) instruction that the 11C does not. It should be possible to make something out of the DSE instruction though. Just replace DSZ with DSE. Since we're dealing with integers both will work. BTW, is there another HP calculator that appeared after 1977 which features DSZ and ISZ instead of the more flexible DSE/ISG commands? Until now I thought that the 19C/29C had been the last ones. (01312018 02:59 PM)damaltor Wrote: this is it: How does it behave if you enter zero ?) Simply add two steps at the beginning: Code: LBL F ...and you're done. Dieter 

01312018, 08:23 PM
Post: #7




RE: Example Program to calculate Factorial
(01312018 12:22 PM)Gamo Wrote: I have changed the program to work for HP11C or any other HP RPN programmable calculator that have conditional test and labels. Any calculator with label addressing and an x=y? test. ;) (01312018 12:22 PM)Gamo Wrote: This intended to show the program step tests as simple as possible to understand. For "as simple as possible" it still looks quite complicated. ;) How about this? Code: 01 LBL A Factorial Think simple. :) Dieter 

01312018, 08:47 PM
(This post was last modified: 01312018 09:19 PM by Thomas Okken.)
Post: #8




RE: Example Program to calculate Factorial
(01312018 08:23 PM)Dieter Wrote: Think simple. :) HP25 style: Code: 01 ABS Make sure input is not negative EDIT: Rearranged code slightly to make the flow of control look a bit cleaner (didn't save any GTOs, though). 

01312018, 10:04 PM
(This post was last modified: 01312018 10:07 PM by Gerson W. Barbosa.)
Post: #9




RE: Example Program to calculate Factorial
(01312018 08:47 PM)Thomas Okken Wrote: HP25 style: 0h, and I thought mine was good enough… At least now I know what I could have done to save yet one more step. Thanks! Done it on my 33C. I didn’t care to make sure the input was an integer or greater than zero as I was not aware of that requirement. Code:


01312018, 10:54 PM
Post: #10




RE: Example Program to calculate Factorial
(01312018 08:47 PM)Thomas Okken Wrote: HP25 style: (01312018 10:04 PM)Gerson W. Barbosa Wrote: 0h, and I thought mine was good enough… At least now I know what I could have done to save yet one more step. Thanks! Then add two steps penalty. ;) Here's my HP25 / HP33E/C version: Code: 01 ABS Make sure input is not negative A bit cleaner than Thomas' version and one step shorter than Gerson's (if you add ABS INT). And finally it even works with calculators that only feature a very limited set of test commands – only x=0? is required. BTW all this is better than HP's own factorial program in the HP25 Applications Programs book (p. 110) which requires one register and does not even handle 0! correctly. Instead it gets stuck in an infinite loop... #) Dieter 

02012018, 01:50 AM
(This post was last modified: 02012018 01:51 AM by Gamo.)
Post: #11




RE: Example Program to calculate Factorial
Dieter program for HP 25 version is very well done that even include ABS and INT. Very short and really simple easy to understand program so far.
I added the ABS steps to work for HP12C and still the program steps is under 20 steps. Code:
Gamo 

02012018, 08:06 PM
Post: #12




RE: Example Program to calculate Factorial
... and HP12C version in engineer style:
The core is 3 steps: Code: 01: LN Preparation for use required to clear statistics registers, store your number (N) in R1, then goto the start of program memory, run the program and you'll get the LN(N!) in R2. You can get N! if you type Code: RCL 2 If you like waste some memory you can program it fully (9 steps): Code: 01: STO 1 Use: 1.) f CLEAR \GS 2.) f CLEAR PRGM 3.) Type N 4.) R/S Benchmark: 69! = 1.711224655E99, running time: 62 secs on "classic" 12C Made in Brazil (S/N: 3107B...) Some hints are welcome to optimize the code (twisted GTO 07/02 really terrible) Csaba 

02022018, 12:59 AM
(This post was last modified: 02042018 03:43 AM by Gerson W. Barbosa.)
Post: #13




RE: Example Program to calculate Factorial
(01312018 08:23 PM)Dieter Wrote: How about this? How about that? Code:
Same number of steps, but you can always can make it shorter :) (not by simply removing line #14, of course). Gerson. PS: Well, you’ve had your chance. In order to make it three steps shorter just remove the redundant steps 03 through 05. Also, instead of using the specific instruction x<>I it’s better to use the more general instructions R/\ and RCL I after GTO 01. But what is the point implementing the factorial function on a calculator that has it already builtin? HP33C/E Code:


02022018, 01:16 AM
Post: #14




RE: Example Program to calculate Factorial
(02012018 08:06 PM)Csaba Tizedes Wrote: Benchmark: But it's wrong! 69! is 1.711224655 E 98 Tom L People may say I'm inept but I consider myself to be totally ept. 

02022018, 02:44 AM
Post: #15




RE: Example Program to calculate Factorial
For historical purposes and ideas...
Here is factorial from V2N10P11 of PPC Journal: 01 STO 0 02 GTO 05 03  04 ST0 x 0 05 1 06 X<Y 07 GTO 03 08 X=Y 09 RCL 00 10 GTO 00 (or R/S) Here is factorial from V3N5P4 01 STO 0 02 1 03  04 STO x 0 05 1 06 X NE Y 07 GTO 03 08 RCL 0 09 GTO 00 Also from V3N5P4 01 ENTER 02 ENTER 03 1 04 X=Y 05 GTO 10 06  07 X 08 LASTX 09 GTO 03 10 RDN 11 RDN 12 GTO 00 

02022018, 03:41 AM
(This post was last modified: 02022018 03:46 AM by Gamo.)
Post: #16




RE: Example Program to calculate Factorial
When we make our own program somehow the internal function compute faster than programs version. Is this due to the advantage of buildin functions is in ROM or algorithm formula program is better than our own program? Like these factorial programs.
For modern 12C+ or 15C LE no speed differences. Gamo 

02022018, 05:05 AM
Post: #17




RE: Example Program to calculate Factorial
(02022018 03:41 AM)Gamo Wrote: When we make our own program somehow the internal function compute faster than programs version. Is this due to the advantage of buildin functions is in ROM or algorithm formula program is better than our own program? Like these factorial programs. It is possible that the ROM uses a better algorithm, but in the case of n!, I think the basic multiplication is used everywhere. The reason user programs are slower than ROM routines is because of the extra work the calculator has to do while executing user code: it has to fetch the user code instructions from memory, then decode and execute them, one by one for each program step. When it is running a ROM routine, only the machine code that performs the calculation is executed; when it is running user code, it has to do all that work *and* the work of decoding and executing the user code instructions. How much that overhead adds up to depends on various details, and can vary a lot. For example, on my HP42S, calculating 253! with the builtin N! function takes about 2 seconds, while the program takes about 12 seconds, so 6 times slower; in Free42, the program is only 1.5 times slower than the builtin function. 

02022018, 07:39 AM
Post: #18




RE: Example Program to calculate Factorial
(02022018 01:16 AM)toml_12953 Wrote:(02012018 08:06 PM)Csaba Tizedes Wrote: Benchmark: Tom, you know, I have a lovely secretary Ms. Type. She's so cute but I must to correct she's typos always. I'll correct it, thanks. By the way your answer clearly shows you don't tried my 3 steps solution. If I posted it in 2003 and if Valentine Albillo had been here he answer something like this: 'Hey, Csaba, this is REALLY interesting idea to implement a counter using statistics. And that' s foolish to make logarithm and summing with \GS' All the above solutions are use the classic 'grab the numbers and make the product of them'. My idea is that, make the logarithm of the product and use the automatic counting of statistics AND make summation IN ONE STEP. Therefore the factorial function CAN BE implement ONLY in 3 steps. The other steps only for nonengineer users. Csaba 

02022018, 08:28 AM
Post: #19




RE: Example Program to calculate Factorial
(02022018 02:44 AM)Gene Wrote: For historical purposes and ideas... That's a nice one. (02022018 02:44 AM)Gene Wrote: Here is factorial from V3N5P4 This one doesn't get 0! right and it even fails for 1!. Both inputs cause an infinite loop. But that's not worse than the solution in the Applications Progams book which fails in the same way – just another example of poor programming by HP themselves. I really wonder if they were not able to do better or if they simply didn't care. #\ (02022018 02:44 AM)Gene Wrote: Also from V3N5P4 And that one also cannot handle 0!. Dieter 

02022018, 08:44 AM
Post: #20




RE: Example Program to calculate Factorial
(02022018 05:05 AM)Thomas Okken Wrote: It is possible that the ROM uses a better algorithm, but in the case of n!, I think the basic multiplication is used everywhere. Now take a real (hardware) HP35s and calculate a few factorials: 100! ... 120! ... 150! ... the calculation takes about one second until the result is displayed. Then continue: 155! ... 157! ... 158! ... again, it takes about 1½ seconds. 159! ... 160! ... and now the answer is returned almost immediately. Up to 253! it gets only slightly slower, finally about half a second is required. So 250! is about twice as fast as 150!. What's going on there? Since the 35s factorial key also handles the Gamma function and Gamma is calculated faster than a simple factorial (yes – try 150! and 150,2!) I suspect this may be related to a fast Gamma code. Dieter 

« Next Oldest  Next Newest »

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