HP30b 38Programming examples in RPN mode
HP30b 38Programming examples in RPN mode
The HP 30b Business Professional calculator includes a programming capability designed to help automate repetitive
calculations and extend the usefulness of the built-in function set of the calculator. The capability includes the creation of
up to 10 separate programs using up to 290 bytes of memory among them.
Programs record keystrokes, with each keystroke using one byte of memory, although some commands use more than
one byte. In addition, many program-only functions are provided for conditional tests, “gotos”, looping, displaying
intermediate results and even calling other programs as subroutines.
This learning module illustrates several example programs to help get you started. While these examples may not be
directly related to what you need the HP 30b to do, they may illustrate how it can be extended by developing and using
programs. These examples may stimulate programming ideas as well.
When a program is displayed, a number will be shown below it indicating how many bytes are used. If the program name
is shown in reverse video, then the program has been assigned to a key and can be executed by pressing the
appropriate key combination, even when in calculation mode. This is shown in the image at right. When viewing a
program in the program catalog, pressing :xI will delete
the presently displayed program and return you to the calculation
environment. To delete all programs, press :x>>
II while in calculation mode.
At different places within a program, you can insert a Label (LBL) command. A label defines a location to which program
control may be transferred. The HP 30b can handle up to 100 labels within the entire program memory. These labels are
a two-digit numeric value from 00 to 99. No label can be used more than once, which makes each label a “global” label
and defined only once within the global program memory space. If you attempt to enter a label that has already been
used, a message saying “Exists!” will be briefly displayed.
This program illustrates an important feature of the HP 30b that is available to programmers: 100 data registers are
available and can be accessed indirectly using data register 0 as an index or pointer. The statistics data registers begin
from one end of this 100 register area and the cash flow values begin from the other end. They cannot overlap, so any
values stored in one data area reduce the available number of registers for the other data area.
For example, if you press 5:$0 and then press 4:$:y. The 5 will be stored in position 6 of
the data registers, which is Y(3). It will be stored in the 6th position because the first position is referenced with an index
of 0. To recall a value from the statistics data registers, store the proper index value into memory register 0 and press
L:y.
To use the cash flow registers, press 5:$0 and then press 4:$C.The 5 will be stored in position
6 of the cash flow registers, which is #CF(2). It will be stored in the 6th position because the first position is referenced
with an index of 0. To recall a value from the cash flow registers, store the proper index value into memory register 0 and
press LC.
This allows for the use of two separate data areas of up to 100 total values, if a programmer wishes.
: + %81 Label 81 is the top of the loop through the statistics registers.
:w7
*
L:y
L*1
+ Numerator is Data(i)*n + carry * 10^7.
:$2 Set up the number of registers for ISG loop in label 81.
L1
2*
1+ Denominator is 2n+1.
:$3
/
:s
:+>
:+>
I Accesses the IP (integer part) function in the math menu.
:$:y IP(Data(i)*n+carry*10^7)/(2n+1)) stored into Data(i).
L*3
L2
) Performs a swap of X and Y, since this program is in RPN mode.
- Carry into the next register.
: + }0 Inserts ISG 0. Checks for the end of the statistics register loop.
: + C81 If not the end, loop back to label 81.
0 These next lines are needed because of the way the loop ends. A 2 is needed in Data(0).
:$0
2
This program takes 154 bytes and has a checksum of 189. This program uses over half of the available program
memory on the HP 30b.
To execute this program, enter the number of registers you wish to use for the results and press =. The first register
will always contain the integer value of PI: a value of 3. The registers after the first one contain the decimal digits of PI,
shown as an integer. Entering 1 to use registers 0 and 1 for storage will compute 7 decimal digits of PI in about 1 second
while a value of 5 (using registers 0 through 5) will compute 35 digits in just a few seconds. The maximum number of
registers that can be used is 99, which uses registers 0 through 99, for 693 digits of PI in under an hour. Also note that
leading zeroes are not shown in the data registers. If the seven digits should be 0000023, the data register would simply
show 23. The user must note and add any leading zeroes. If run with 5 as the number of registers to be used, the
program ends with the following displayed. Press < to see additional results as shown below.
Given a number, this program will return a series of prime factors. After each factor is returned, press : + ( (which
executes a R/S command) to continue the factoring of the number. If the original number is displayed, then the original
number is prime. The program presented below MUST be run in RPN mode.
L2 Fractional part was non-zero. Number in memory 1 is not a factor. Increment factor to try next
:$+1 by recalling value in memory 2 and adding it to value in memory 1.
2
:$2 These steps ensure the factor increment is 2, since the loop starts with this at a value of 1.
L1
:m Trial factor squared. If larger than number being factored, stop the loop.
L0 Number being factored.
:+M Inserts a ?<= conditional test. If the value of memory 1, squared, is less than memory 0, places a
1 in the X register. Otherwise, places a 0 in the X register.
: + R00 Inserts a Goto True command. If X is not equal to 0, go to label 00.
1
L0 Compare last factor found to 1.
:+N Inserts a ?= conditional test. If the value of memory 0 is equal to 1, places a 1 in the X register.
Otherwise, places a 0 in the X register.
: + R01 Inserts a Goto True command. If X is not equal to 0, go to label 01.
:+( Inserts R/S command and displays the present prime factor.
hp calculators -7- HP 30b Programming examples in RPN mode - Version 1.0
hp calculators
This program takes 59 bytes and has a checksum of 247. To execute this program from the program catalog, enter the
number you wish to factor and press ==. If you have left the program catalog, reenter it by pressing : .
Question 1: What are the prime factors of 55? Key in 55 and press ==.
0 is displayed. This indicates the factors have been found. The prime factors of 55 are 5 and 11.
Question 2: What are the prime factors of 9999999967? Enter the program catalog by pressing pressing : .
Key 9999999967 and press ==. Be aware that this will take several minutes to run.
The original number 9999999967 is displayed as the first factor found. Press : + ( to continue.
0 is displayed. This indicates the factors have been found. 9999999967 is prime.
Historical note: The HP user club (not associated with Hewlett Packard) that came to be known as PPC published a
journal for many years that included programs written by users. One such program was a “Speedy Factor Finder”. The
value used as a test case for speed improvements was the largest 10-digit prime number, 9999999967. This number
proved prime using a program written for the HP 67 calculator in just under 3 hours.
Inputs to this program are the number to convert, the input base, and the output base. The program presented below
MUST be run in RPN mode.
This program takes 54 bytes and has a checksum of 155. To execute this program from the program catalog, enter the
number you wish to convert, press =, enter the number’s present base, =, and enter the base you wish to convert
it to and press ==.
Convert 175 base 8 to base 10. Key in 175=8=10==.
175 base 8 is equal to 125 base 10. Now convert this result to base 2. Since when executed, the program leaves the
program catalog, to run it again press: : 125=10=2==.
This example program simulates landing on the moon. It was originally published by Hewlett Packard in 1975 and can
be found in the HP 25 Applications Program book.
The game starts off with the rocket descending at a velocity of 50 feet/sec from a height of 500 feet. The velocity and
height are shown in a combined display as -50.0500, the height appearing to the right of the decimal point and the
velocity to the left, with a negative sign on the velocity to indicate downward motion. If a velocity is ever displayed with
no fractional part, for example, -15, it means that you have crashed at a speed of 15 feet/sec. In game terms, this means
that you have lost; in real-life, it signifies an even less favorable outcome.
You will start the game with 120 units of fuel. You may burn as much or as little of your available fuel as you wish (as
long as it is an integer value) at each step of your descent; burns of zero are quite common. A burn of 5 units will just
cancel gravity and hold your speed constant. Any burn over 5 will act to change your speed in an upward direction. You
must take care, however, not to burn more fuel than you have; for if you do, no burn at all will take place, and you will
free-fall to your doom! The final velocity shown will be your impact velocity. Any impact velocity over 5 feet/sec would
probably doom your attempt. You may display your remaining fuel at any time by recalling memory 2.
500
:$0 Store the initial height in memory 0.
50}
:$1 Store the initial downward velocity in memory 1.
120
:$2 Store the initial fuel in memory 2.
304 This executes a roll down of the 4-level stack.
:$:u Set mode to RPN and 4 decimal places shown.
( These steps are performed when velocity is positive. Performs a stack roll down.
+ Adds velocity to fraction displaying height.
: + C33 Inserts a Goto command. Jumps to label 33.
: + %31 Label 31. These steps are performed when velocity is negative.
( Performs a stack roll down.
) Performs a stack swap of the X and Y registers.
- Subtracts a negative velocity from a positive height.
: + %33 Label 33. Destination after alternate paths for positive or negative velocity.
:+( Inserts R/S command and displays V.X, velocity.height
L2
:+A Inserts a ?> conditional test. If the input burn is greater than amount of fuel, prepare to crash.
: + R34 Inserts a Goto True command. If X is equal to 0, go to label 34 and prepare to crash.
( Performs a stack roll down. Burn is less than total fuel. Update acceleration, velocity, and height.
:$-2 Subtract burn from fuel.
5- 5 units cancels effects of gravity, so acceleration is burn minus 5.
:$3 Store acceleration into memory 3.
2
/
L0
+
L1
+ New height = original height plus velocity plus acceleration.
:$0 Store new height into memory 0.
0 Compare height to 0.
:+V Inserts a ?< conditional test. If the height is less than 0, places a 0 in the X register. Otherwise,
places a 1 in the X register.
: + R35 Inserts a Goto True command. If X is equal to 0, go to label 35. Label 35 represents a crash with
fuel remaining.
This program takes 105 bytes and has a checksum of 121. To run this program from the program catalog, press =.
The initial descent display is shown. The landing craft is 500 feet high and descending at 50 feet / sec. Burn 5 units of
fuel by pressing 5: + (.
This program takes 59 bytes and has a checksum of 192. To play the game, press = while in the program catalog.
Enter a guess of 40. 40: + (. Note: since the number generated will be random, the game play illustrated
below will probably not match your own experience, since a different secret number will probably be generated.