# 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 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, as described later. In addition, many program-only 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 program-only functions. Other than the Black-Scholes 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.

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.

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.

## Example 1: Calculating digits of PI

The first example program will compute a user-specified 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.

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.

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.

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

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.

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 10-digit 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.

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 .

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:

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 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.

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 .

Check remaining fuel by pressing .

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.

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.

The secret number was 92 and was found in 5 guesses!

### HP Support forums

Find solutions and collaborate with others on the HP Support Forum