Example Program to calculate Factorial
02-02-2018, 08:53 AM
Post: #21
 Dieter Senior Member Posts: 2,258 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 01:16 AM)toml_12953 Wrote:  But it's wrong!

69! is 1.711224655 E 98

No, 69! is 1,711224524 E+98.

The result calculated by the program is off in its last three digits. This is caused by the summation of the logs (the sum can and will be off in the last digit(s)) and the final exponential which increases the error.

Dieter
02-02-2018, 10:02 AM (This post was last modified: 02-02-2018 10:03 AM by Csaba Tizedes.)
Post: #22
 Csaba Tizedes Member Posts: 292 Joined: May 2014
RE: Example Program to calculate Factorial
(02-02-2018 09:06 AM)Mike (Stgt) Wrote:
(02-01-2018 08:06 PM)Csaba Tizedes Wrote:  ... and HP-12C version in engineer style:

The core is 3 steps:
...and so on -- nice!

I assume, the following HP-12C one stepper is too short to compete, in lacks the brain fuck.
Code:
01-    43  3

In addition, the result for 69 is about 1.31E91 away from yours.

Ciao.....Mike

Hey, I want to crack this joke...

Csaba
02-02-2018, 01:53 PM
Post: #23
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 08:28 AM)Dieter Wrote:
(02-02-2018 02:44 AM)Gene Wrote:  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)

That's a nice one.

Except that

1) I cannot find X<Y anywhere on my HP-33C;

2) The stack is not properly handled.

What calculator was that program written for?

Gerson.
02-02-2018, 03:15 PM
Post: #24
 Thomas Okken Senior Member Posts: 633 Joined: Feb 2014
RE: Example Program to calculate Factorial
(02-02-2018 08:44 AM)Dieter Wrote:
(02-02-2018 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.

It sounds like it may have a hard-coded value for 160!, so calculations for n >= 160 would require only n - 160 multiplications. That could have been done keep the calculation time from becoming too long, or to keep the error from growing beyond the guard digits.

(02-02-2018 01:53 PM)Gerson W. Barbosa Wrote:  1) I cannot find X<Y anywhere on my HP-33C;

2) The stack is not properly handled.

What calculator was that program written for?

The HP-25 has X<Y. (Hmm, was it the only HP to have that function until the 41C?)
02-02-2018, 03:28 PM
Post: #25
 Gene Moderator Posts: 845 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 01:53 PM)Gerson W. Barbosa Wrote:  Except that

1) I cannot find X<Y anywhere on my HP-33C;

2) The stack is not properly handled.

What calculator was that program written for?
Gerson.

V2N10 of PPC Journal dates from December of 1975 and this program was written for the HP 25 as shown on that page. A real shame HP kept flipping the included conditional tests back and forth back then. A real pain and one of the real cross-model design failures IMO.

And... if anyone here does NOT have the full PDF set of the PPC Journals... why not?

Contact Jake Schwartz. Tons of good material still there.
02-02-2018, 06:59 PM
Post: #26
 Dieter Senior Member Posts: 2,258 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 03:28 PM)Gene Wrote:  A real shame HP kept flipping the included conditional tests back and forth back then. A real pain and one of the real cross-model design failures IMO.

That's something I never understood. Was ROM space or the number of available opcodes really so limited that the full set of 12 test commands could not be realized? In the late Seventies the standard set had only eight of them. But while x<0? exists there is no x<y?, and while there is an x≤y? the corresponding x≤0? is missing. Eventually the 41C then featured ten tests, but it still lacked the two for "greater or equal". Weird.

If there really was room for only eight tests HP better had chosen <, >, = and ≠. From these the missing ≤ and ≥ tests can easily be synthesized: the combination x≠y? x>y? is logically the same as x≥y?.

But who cares today?
Tempi passati. ;-)

Dieter
02-02-2018, 07:20 PM
Post: #27
 Thomas Okken Senior Member Posts: 633 Joined: Feb 2014
RE: Example Program to calculate Factorial
(02-02-2018 06:59 PM)Dieter Wrote:
(02-02-2018 03:28 PM)Gene Wrote:  A real shame HP kept flipping the included conditional tests back and forth back then. A real pain and one of the real cross-model design failures IMO.

That's something I never understood. Was ROM space or the number of available opcodes really so limited that the full set of 12 test commands could not be realized? In the late Seventies the standard set had only eight of them. But while x<0? exists there is no x<y?, and while there is an x≤y? the corresponding x≤0? is missing. Eventually the 41C then featured ten tests, but it still lacked the two for "greater or equal". Weird.

If there really was room for only eight tests HP better had chosen <, >, = and ≠. From these the missing ≤ and ≥ tests can easily be synthesized: the combination x≠y? x>y? is logically the same as x≥y?.

ROM space was definitely limited. The HP-25 ROM is full to the last byte. This is also the first HP to provide comparisons with zero, which probably explains why it's the only one until the 41C where the set of comparisons with zero exactly matches the set of comparisons with Y... and the lack of ROM space could also explain why the inequalities are each other's logical opposites, because that saves another couple of steps in their implementation. And finally, the full set of 12 as it exists on the 42S would have been hard to fit on the HP-25 keyboard. The only thing that the 25 had absolutely no shortage of was opcodes.

(02-02-2018 06:59 PM)Dieter Wrote:  But who cares today?
Tempi passati. ;-)

Well, we do. Duh.
02-02-2018, 07:28 PM
Post: #28
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 03:28 PM)Gene Wrote:  V2N10 of PPC Journal dates from December of 1975 and this program was written for the HP 25 as shown on that page. A real shame HP kept flipping the included conditional tests back and forth back then. A real pain and one of the real cross-model design failures IMO.

And... if anyone here does NOT have the full PDF set of the PPC Journals... why not?

Contact Jake Schwartz. Tons of good material still there.

Thanks, Thomas and Gene!

Yes, it’s there. Listing title: IMPROVED FACTORIAL FUNCTION (0<=n<=69)

I’m sure I have an old version of Jake Schwartz’s DVD, but I don’t know where it is (time for an upgrade, perhaps). But I’ve found it on the “HP-41 EXTENDED ARCHIVE DVD”, from TOS.

Gerson.
02-02-2018, 07:52 PM (This post was last modified: 02-02-2018 07:55 PM by Gerson W. Barbosa.)
Post: #29
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 06:59 PM)Dieter Wrote:  Eventually the 41C then featured ten tests, but it still lacked the two for "greater or equal".

The full 12-test set first appeared on the HP-15C, I think. Even better, they took up only three positions on the keyboard: x<=y, x=0 and TEST.

02-02-2018, 08:16 PM
Post: #30
 badaze Member Posts: 70 Joined: Nov 2017
RE: Example Program to calculate Factorial
7 steps but does not manage 0!
HP 29C

Code:
 STO 0 1 LBL 0 RCL 0 x DSZ GTO 0

My site http://www.emmella.fr
02-02-2018, 08:19 PM (This post was last modified: 02-03-2018 09:36 AM by Dieter.)
Post: #31
 Dieter Senior Member Posts: 2,258 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 07:20 PM)Thomas Okken Wrote:  ROM space was definitely limited. The HP-25 ROM is full to the last byte.

Edit: Full to the last byte? There are 16 (!) NOPs in the listing. Once even five in a row. ;-)
And finally after 40 years they have been put to good use, fixing a strange behaviour in the HP25 number entry routine.

(02-02-2018 07:20 PM)Thomas Okken Wrote:  And finally, the full set of 12 as it exists on the 42S would have been hard to fit on the HP-25 keyboard.

OK, I see the problem with limited ROM space. But there's plenty of room on the keyboard – there are eight (!) unused locations. STO and RCL even do not have any shifted functions at all. But if there is no room for functions to fill these positions... #-)

Dieter
02-02-2018, 09:58 PM (This post was last modified: 02-02-2018 11:02 PM by Gerson W. Barbosa.)
Post: #32
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 08:16 PM)badaze Wrote:  7 steps but does not manage 0!

Try adding EEX as your first step. If it works that’ll make 8 steps. Still pretty good!

PS: It will work only for x=0 and x=1. I have a dead 29C in front of me and can’t think of anything better than [ x=0 ] [ e^x ], which makes 9 steps. Still not bad.
02-03-2018, 01:29 AM
Post: #33
 Gamo Senior Member Posts: 378 Joined: Dec 2016
RE: Example Program to calculate Factorial
I think HP-15C is the most fully pack functions and features after the 41C.

I don't know why HP didn't put Factorial function for HP 25 since this model have very limited program steps and if your program need factorial that will take up more steps in the process.

Why not just put n! instead of x^2

Gamo
02-03-2018, 03:43 AM (This post was last modified: 02-03-2018 03:45 AM by Gerson W. Barbosa.)
Post: #34
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-03-2018 01:29 AM)Gamo Wrote:  I don't know why HP didn't put Factorial function for HP 25 since this model have very limited program steps and if your program need factorial that will take up more steps in the process.

Why not just put n! instead of x^2

Precisely because of the very limited number of program steps in the HP-25, as you’ve noticed. The latter is a more frequently used function than the former. Thus, even if the ROM weren’t full, the designers would probably have chosen x^2 instead of n!. If you have only 49 or 50 steps, then [ ENTER ] [ * ] instead of simply [ x^2 ] will make a difference, depending on how many times you have to use that in a program.

On the other hand, most of the times we won’t need a full implementation of the factorial function. If you are sure no argument will be zero, then you don’t have to worry about that special case, and save one step or two. I remember once I needed factorial in a 33C program, but I had only five steps left. Luckily enough, the possible arguments lay in the 0 through 4 range, then I could use the following:

x^2
5
/
e^x
INT
02-03-2018, 08:56 AM (This post was last modified: 02-03-2018 09:21 AM by Dieter.)
Post: #35
 Dieter Senior Member Posts: 2,258 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 09:58 PM)Gerson W. Barbosa Wrote:  Try adding EEX as your first step. If it works that’ll make 8 steps. Still pretty good!

PS: It will work only for x=0 and x=1. ...

On the HP25 it also works for other input. This seems to be related to a bug, err... "special feature" that has been discussed earlier: R/S obviously does not terminate number entry so that the EEX is appended to the entered number.

But this only works if you manually type your input. You can't recall it from a register, like [RCL] 2 [R/S]. ;-)

(02-02-2018 09:58 PM)Gerson W. Barbosa Wrote:  I have a dead 29C in front of me and can’t think of anything better than [ x=0 ] [ e^x ], which makes 9 steps.

Code:
01  STO 0 02  1 03  x>y? 04  STO 0 05  LBL 0 06  RCL 0 06  x 07  DSZ 09  GTO 0

Same number of steps but a tiny bit faster as no transcendental function is required.

Dieter
02-04-2018, 02:52 AM
Post: #36
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-02-2018 08:28 AM)Dieter Wrote:
(02-02-2018 02:44 AM)Gene Wrote:  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)

That's a nice one.

Code:
 01   1 02   STO 0 03   STO* 0 04   1 05   + 06   x<=y 07   GTO 03 08   RCL 00 09   GTO 00

However, for practical purposes I would insert two Rv instructions after GTO 03:

Code:
 ... 07   GTO 03 08   Rv 09   Rv 10   RCL 0 11   GTO 00

Example:

Compute C(15, 4)

15 R/S 4 R/S / 15 ENTER 4 - R/S / -> 1365

Gerson.
02-08-2018, 08:04 PM (This post was last modified: 02-08-2018 10:06 PM by Gene.)
Post: #37
 Gene Moderator Posts: 845 Joined: Dec 2013
RE: Example Program to calculate Factorial
Slightly OT, but wanted to put this into the factorial thread. Source: A TI SR-56 brochure printed in England.

Factorial for the SR-56. Takes 14 steps out of 100. Thats' an interesting efficiency aspect vs. the HP 25, etc. versions.

Code:
LRN             (places calculator in program mode) 00  STO 01  0           (stores n into memory 0 - dsz always works on memory 0) 02  2nd CP   (clear's the T-register) 03  x=t?      (Tests if zero entered for n?) 04  1 05  2           (if so, transfers to location 12 to display 1 as answer) 06  RCL       (if not, continues here) 07  0           (Recalls N and starts the multiplication loop) 08  x  09  dsz        (As long as memory 0 is > 0, loops back to step 06) 10  0 11  6 12  1           (when memory 0 is equal to zero, multiplies computed value by 1) 13  =          ( then finishes computation and stops execution) 14  R/S
02-09-2018, 01:04 AM (This post was last modified: 02-09-2018 01:05 AM by Gerson W. Barbosa.)
Post: #38
 Gerson W. Barbosa Senior Member Posts: 1,074 Joined: Dec 2013
RE: Example Program to calculate Factorial
(02-08-2018 08:04 PM)Gene Wrote:  Factorial for the SR-56. Takes 14 steps out of 100. Thats' an interesting efficiency aspect vs. the HP 25, etc.

When I started using calculators, factorial was not an issue as they all had it built-in. The HP-15C which replaced my TI-59 even had Gamma!

The HP-25C is tricky because of the number-entry bug (or feature) and the lack of x<=y test. The only 10-step program I came up with is not practical as it won’t work for arguments that are results of an operation.
Code:
 01 EEX 02 STO 0 03 1  04 STO* 0 05 1 06 + 07 x<y 08 GTO 04 09 RCL 0 10 GTO 00

0 R/S -> 1.00
1 R/S -> 1.00
5 R/S -> 120.00

but

5 ENTER R/S -> 1.00

On a 25C, I would use this one instead:

Code:
 01 x=0 02 e^x 03 STO 0 04 1 05 - 06 x=0 07 GTO 10 08 STO* 0 09 GTO 04 10 Rv 11 RCL 0 12 GTO 00

Why is 6 afraid of 7?

6 R/S 7 R/S * 10 R/S - -> 0

No, not because of that :-)
02-13-2018, 03:39 PM
Post: #39
 Gene Moderator Posts: 845 Joined: Dec 2013
RE: Example Program to calculate Factorial
Another entry for historical purposes.

From PPC Journal V10N3P26 by Kendrick Chan in the Philippines. Works for 0 < X < 70

01 INT
02 LASTX
03 1
04 -
05 x=0?
06 GTO 09
07 *
08 GTO 02
09 Roll Down
02-22-2018, 01:11 AM
Post: #40
 Valentin Albillo Member Posts: 250 Joined: Feb 2015
RE: Example Program to calculate Factorial
.
Hi all,

A little late but I couldn't resist. This is my attempt at a factorial program which was one of the first programs I wrote some 40+ years ago after I just bought a new, shiny HP-25:

01 STO 0      01     23 00
02 STO/ 0     02  23 71 00
03 STOx 0     03  23 61 00
04 1          04        01
05 -          05        41
06 X#0?       06     15 61
07 GTO 03     07     13 03
08 RCL 0      08     24 00

to run it, and a few selected cases:

f PRGM, FIX 0

1 R/S -> 1
5 R/S -> 120
13 R/S -> 6227020800
69 R/S -> 1.7112245 98 (1.711224522e98 internally)

0 R/S -> Error
70 R/S -> OF (Overflow) RCL 0 -> 9.9999999 99

There's no need for a 09 GTO 00 step because the HP-25 fills up the entire 49-step program memory with GTO 00 instructions which are there whether you key them in or not.

As can be seen in the examples it will work for N=1 to 69, but not for 0. I found it very easy to remember and so would use it to show off on the fly my new wonderful HP-25 to people who would ask me what it could do when programmed (no HP-25C back then). They were always extremely amazed that an useful program would be so easily entered and run on the go.

V.
.
 « Next Oldest | Next Newest »

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