hpsupportheadportlet
Actions ${title}
Loading...
HP Customer Support
hpcontactsecondarynavigationportlet
Actions ${title}
Loading...
hpherosupportsearch
Actions ${title}
Loading...
hpshareprintwidgetportlet
Actions ${title}
Loading...
hpconcentrawrapperportlet
Actions ${title}
Loading...
HP 30b Business Professional Calculator  Programming Examples in RPN Mode
Programming on the HP 30b
The HP 30b Business Professional calculator includes a programming capability designed to help automate repetitive calculations and extend the usefulness of the builtin 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, as described later. In addition, many programonly functions are provided for conditional tests, conditional and unconditional 'gotos', looping, displaying intermediate results and even calling other programs as subroutines.
This learning module will cover using loops and subroutines in the HP 30b programming environment in some detail. Other learning modules will show how to enter and edit programs, how to automate short, repetitive tasks, as well as showing several example programs to help get you started.
As shown in the picture, the HP 30b has additional functions assigned to the keys that are programonly functions. Other than the BlackScholes function (shown as Black S), which is not a program function but a financial function, these functions are not printed or labeled on the actual HP 30b itself. However, an overlay is provided that lays over the top rows of keys that help indicate how these functions are mapped to the keys.
Figure : HP 30b Business Professional Calculator
Each of these functions is inserted into a program by pressing the shift key and holding it down while pressing the key under which the program function is displayed. For example, to insert a LBL (label) command, press
and, while holding it down, press
. In these learning modules describing programming, this will be shown as
. Pressing that key combination will insert a LBL instruction into a program in program edit mode. Pressing that key combination in calculation mode will do nothing.
There are 10 numbered slots available for programs, numbered from 0 to 9. These are displayed in the program catalog which is viewed by pressing
. In the image above, the program catalog is displayed, showing Prgm 0 or program 0. Pressing the
or
keys will scroll through the list of 10 programs. Pressing
will enter the selected program, allowing you to view the program steps stored in that program slot or to change the program steps. To exit this program editing mode and return to the program catalog, press
. To exit the program catalog and return to calculation mode, press
.
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 below. When viewing a program in the program catalog, pressing
will delete the presently displayed program and return you to the calculation environment. To delete all programs, press
while in calculation mode.
Figure : Viewing a program
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 twodigit 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.
Example 1: Calculating digits of PI
The first example program will compute a userspecified number of digits of the constant PI and place the result in the cash flow / statistics data registers. It uses Euler’s convergence improvement applied to the Gregory series for PI.
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
and then press
. 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
.
To use the cash flow registers, press
and then press
.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
.
This allows for the use of two separate data areas of up to 100 total values, if a programmer wishes.
Keys Pressed

Explanation


Enters program mode and displays the last program previously viewed in the program catalog. If you wish to enter your program into a different program number in the catalog, press


Enters program edit mode and displays the first line of the program


Save the number of registers to fill.


Save the user’s mode to restore at end of program.


Set mode to RPN and fix 0 decimal places


Clear the cash flow registers


Clear the statistics registers. This provides maximum room for the statistics registers where the results will be stored.


Recall the number of registers to fill.


Multiply by 14, which is 7 digits per register x 2 (which is the loop increment).


Number of digits ÷ log base 2 of 10 are the number.of iterations needed (and the count down is by 2).


Accesses the IP (integer part) function in the math menu.


Saves the number of loops required in memory 1.


Label 80 is the top of the main loop.


Set up the number of registers for ISG loop in label 81.


Initial value of term.


Initial value for carry.


Label 81 is the top of the loop through the statistics registers.


Numerator is Data(i)*n + carry * 10^7.


Set up the number of registers for ISG loop in label 81.


Denominator is 2n+1.


Accesses the IP (integer part) function in the math menu. IP(Data(i)*n+carry*10^7)/(2n+1)) stored into Data(i).


Performs a swap of X and Y, since this program is in RPN mode.


Carry into the next register.


Inserts ISG 0. Checks for the end of the statistics register loop.


If not the end, loop back to label 81.


These next lines are needed because of the way the loop ends. A 2 is needed in Data(0).


Inserts DSE 1. Checks for the end of the term loop. If you add these two instructions BEFORE the DSE 1 at this step, you will see a 'countdown' displayed as the program executes this loop:


If not the end, loop back to label 80.


This is the final 'fix up' loop. Just one pass through the registers to adjust the overflows.


Initial value of the carry.


Label 82 is the top of the loop through the statistics registers to adjust for any overflow.


Add carry to register.


Inserts ?<conditional test. If true, no overflow.


If true, skip over adjustment.


Back out overflow.


Carry into next register.


Label 83.


No carry.


Inserts DSE 0. Decreases register pointer and loops until all have been checked.


Loop over. Clean up by restoring user’s original mode settings.


Program will end showing the cash flow registers to allow for review.


Inserts Stop. Program over.


Exits program edit mode and returns to the program catalog.

Figure : Displays the program memory
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.
Figure : Displays the first register
Figure : Displays the second register
Figure : Displays the third register
Figure : Displays the fourth register
Figure : Displays the fifth register
Figure : Displays the sixth register
To 35 decimals, the value of PI is 3.14159265358979323846264338327950288.
Example 2: Finding prime factors of an integer
Don would like to develop a program to factor some numbers into their prime factors. This example program will find the prime factors of an integer. For example, the number 10 can be factored into the product of two primes, 2 and 5. The number 13 is prime, as it can only be factored into 1 and 13.
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.
Keys Pressed

Explanation


Enters program mode and displays the last program previously viewed in the program catalog. If you wish to enter your program into a different program number in the catalog, press


Enters program edit mode and displays the first line of the program.


Store number to be factored in memory 0.


Memory 1 stores the trial factor to use. Start with 2.


Memory 2 stores the increment to the trial factor. Starts with 1 to make 2nd factor tried equal to 3, then 2 to try 5, 7, 9…


Label 00 is the main loop.


Accesses the FP (fractional part) function in the math menu. If 0, found a factor in memory 1.


Inserts a Goto False command. If the result of the FP instruction is zero, go to label 02.


Fractional part was nonzero. Number in memory 1 is not a factor. Increment factor to try next by recalling value in memory 2 and adding it to value in memory 1.


These steps ensure the factor increment is 2, since the loop starts with this at a value of 1.


Trial factor squared. If larger than number being factored, stop the loop.


Number being factored.


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.


Inserts a Goto True command. If X is not equal to 0, go to label 00.


Compare last factor found to 1.


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


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.


Label 01 is the destination if the last factor was 1.


Inserts a Stop command and displays a 0. Indicates all prime factors have been found


Label 02 indicates a prime factor was found.


Display factor found.


Inserts a R/S command and displays the current factor.


Update new number to factor by dividing number by factor found.


Inserts a Goto 00 command. Continues the loop.


Exits program edit mode and returns to the program catalog.

Figure : Displays the program memory
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
.
Figure : Displays the first round factor
5 is displayed as the first factor found. Press
to continue.
Figure : Displays the second round factor
11 is displayed as the next factor found. Press
to continue.
Figure : Indicates that all the factors are found
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
.
Key 9999999967 and press
. Be aware that this will take several minutes to run.
Figure : Displays the first round factor
The original number 9999999967 is displayed as the first factor found. Press
to continue.
Figure : Indicates that all the factors are found
0 is displayed. This indicates the factors have been found. 9999999967 is prime.
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 10digit prime number, 9999999967. This number proved prime using a program written for the HP 67 calculator in just under 3 hours.
Example 3: Base conversions
This example program converts a number from a base to another base in the range of bases 2 through 10, provided that one of the bases is in fact 10. For example, this program can convert from base 10 to a base 2 through 9, or can convert from a base 2 through 9 to base 10. To convert a number in base 8 to base 2, for example, you must perform an intermediate step by first converting the base 8 number to base 10 and then converting the resulting base 10 number to base 2. Bases greater than 10 are not supported by this program.
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.
Keys Pressed

Explanation


Enters program mode and displays the last program previously viewed in the program catalog. If you wish to enter your program into a different program number in the catalog, press


Enters program edit mode and displays the first line of the program.


Store the output base in memory 2.


This executes a roll down of the 4level stack.


Store the input base in memory 1.


This executes a roll down of the 4level stack.


Store the number to convert in memory 0.


Initialize the output number in memory 3.


Initialize the multiplier to use in memory 4.


Label 10 is the main loop.


Divide number to convert by output base.


Store result back into memory 0.


Accesses the FP (fractional part) function in the math menu.


Multiply fractional part by output base to get digit.


Multiply by digit position multiplier and


add to accumulated total.


Update multiplier by multiplying by


the input base.


Accesses the IP (integer part) function in the math menu. Takes the integer part of the earlier computed quotient.


Store the result back into memory 0.


Inserts a Goto True command. If the integer part is not equal to zero, go to label 10. This will continue the loop until the quotient is zero.


Recall final output number in new base.


Inserts a Stop command. Program ends execution.


Exits program edit mode and returns to the program catalog.

Figure : Displays the program memory
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
.
Figure : Converting 175 base 8
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:
Figure : Calculating 125 base 10
175 base 8 is equal to 125 base 10 which is equal to 1111101 base 2.
Example 4: Lunar lander game
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 reallife, 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 freefall 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.
Keys Pressed

Explanation


Enters program mode and displays the last program previously viewed in the program catalog. If you wish to enter your program into a different program number in the catalog, press


Enters program edit mode and displays the first line of the program.


Store the initial height in memory 0.


Store the initial downward velocity in memory 1.


Store the initial fuel in memory 2.


This executes a roll down of the 4level stack.


Set mode to RPN and 4 decimal places shown.


Label 30 is the main loop.


Divide height by 10,000.


Compare velocity to 0.


Inserts a?<conditional test. If the velocity is less than 0, places a 0 in the X register. Otherwise, places a 1 in the X register.


Inserts a Goto True command. If X is equal to 0, go to label 31. Label 31 is when velocity is negative.


These steps are performed when velocity is positive. Performs a stack roll down.


Adds velocity to fraction displaying height.


Inserts a Goto command. Jumps to label 33.


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.


Label 33. Destination after alternate paths for positive or negative velocity.


Inserts R/S command and displays V.X, velocity.height


Inserts a ?< conditional test. If the input burn is greater than amount of fuel, prepare to crash.


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.


Subtract burn from fuel.


5 units cancels effects of gravity, so acceleration is burn minus 5.


Store acceleration into memory 3.


New height = original height plus velocity plus acceleration.


Store new height into memory 0.


Compare height to 0.


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.


Inserts a Goto True command. If X is equal to 0, go to label 35. Label 35 represents a crash with fuel remaining.


Have not crashed. Performs a stack roll down.


Recall acceleration.


Store new velocity into memory 1.


Inserts a Goto command. Jumps to label 30 to begin another loop.


Label 34. Determines velocity of crash with no fuel remaining.


Compute crash velocity as square root of (V^{2} + 2gHeight), where g=5


Display as a negative number to indicate a crash.


Label 35. Create display to indicate a crash occurred.


Set mode to RPN and 0 decimal places shown for a crash.


Recall crash velocity.


Inserts a Stop command. Program ends execution.


Exits program edit mode and returns to the program catalog.

Figure : Displays the program memory
This program takes 105 bytes and has a checksum of 121. To run this program from the program catalog, press
.
Figure : Velocity of the flight
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
.
Figure : Burning 5 units of fuel
Burn 0 units of fuel by pressing
.
Figure : Burning 0 units of fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Burn 5 units of fuel by pressing
.
Figure : Burning 5 units of fuel
Burn 0 units of fuel by pressing
.
Figure : Burning 0 units of fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Check remaining fuel by pressing .
Figure : Calculating remaining fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Burn 5 units of fuel by pressing
.
Figure : Burning 5 units of fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Burn 10 units of fuel by pressing
.
Figure : Burning 10 units of fuel
Burn 15 units of fuel by pressing
.
Figure : Burning 15 units of fuel
This is a crash. Perhaps you can do better?
Example 5: Guess the secret number game
This program generates a secret number between 0 and 99. The user enters a guess and the program indicates whether the guess is too high or too low. This looping process continues until you guess the number. By making proper guesses, any number can be found in 7 or fewer attempts.
Keys Pressed

Explanation


Enters program mode and displays the last program previously viewed in the program catalog. If you wish to enter your program into a different program number in the catalog, press


Enters program edit mode and displays the first line of the program.


Get random seed.


Multiply by 100.


Accesses the integer part (IP) function from Math menu. Displayed as 'Math Up Up ='


Initialize guess counter at 0.


Label 70. Main loop of program.


Inserts a R/S command. Enter your guess between 0 and 99.


Necessary to terminate digit entry of guess.


Increment guess counter.


Roll the stack down.


Swap. Puts your guess in X and secret number in Y.


Inserts a not equal conditional test.


Inserts a Goto True command. If the guess is not the secret number, go to label 71.


Recall guess count.


Insert a MSG command to display 'Yes'


Inserts a Y.


Inserts an e.


Inserts an s.


Terminates MSG character entry.


Inserts a Stop command. Game over. Number of guesses is in display.


Your guess was wrong.


Inserts ?> conditional test.


Inserts a Goto True command. If the secret number is greater than the guess, go to label 72.


Insert a MSG command to display 'High'


Inserts an H.


Inserts an i.


Inserts a g.


Inserts an h.


Terminates MSG character entry.


Go back for another guess.


Your guess was too low.


Insert a MSG command to display 'Low'


Inserts an L.


Inserts an o.


Inserts a w.


Terminates MSG character entry.


Go back for another guess. This is line 32 of the program.


Exits program edit mode and returns to the program catalog.

Figure : Displays the program memory
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.
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.
Figure : Entering 40
40 is too low. Enter a guess of 90.
.
Figure : Entering 90
90 is also too low. Enter a guess of 97.
.
Figure : Entering 97
97 is too high. Enter a guess of 95.
.
Figure : Entering 95
95 is too high. Enter a guess of 92.
.
Figure : Entering 92
The secret number was 92 and was found in 5 guesses!
hpfeedbackinputportlet
Actions ${title}
Loading...
hponlinecommunitiesportlet
Actions ${title}
Loading...
Ask the community!
hpfeedbackbannerportlet
Actions ${title}
Loading...
hpcountrylocatorportlet
Actions ${title}
Loading...
United States
hpdetectloadmydeviceportlet
Actions ${title}
Loading...