Post Reply 
Example Program to calculate Factorial
02-02-2018, 12:59 AM (This post was last modified: 02-04-2018 03:43 AM by Gerson W. Barbosa.)
Post: #13
RE: Example Program to calculate Factorial
(01-31-2018 08:23 PM)Dieter Wrote:  How about this?

Code:
01  LBL A   Factorial 
02  ABS     Make sure input is not negative
03  INT     Make sure input is an integer
04  X=0?    If input is zero...
05  1       ...then change it into 1 (0! = 1! = 1)
06  STO 1   Store input n as initial product
07  LBL 3   Start of loop
08  1   
09  -       decrement factor
10  X=0?    has factor become zero?
11  GTO 2   then quit program
12  STOx1   else multiply product by factor
13  GTO 3   and do another loop
14  LBL 2   exit routine:
15  RCL 1   Recall result
16  RTN     and quit

How about that?

Code:

01 LBL A
02 ABS
03 INT
04 x=0
05 e^x
06 1
07 STO I
08 LBL 1
09 STO* I
10 1
11 +
12 x<=y
13 GTO 1
14 Rv       
15 x<>I
16 RTN

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 built-in?

HP-33C/E

Code:


01 ABS             ; optional
02 1
03 STO 0
04 STO* 0
05 1
06 +
07 x<=y
08 GTO 04
09 Rv              ; optional 
10 Rv              ; optional 
11 RCL 0
12 GTO 00
[/quote]
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Example Program to calculate Factorial - Gerson W. Barbosa - 02-02-2018 12:59 AM



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