HP Customer Support - Knowledge Base

# HP 20s Calculator - Pythagorean and RNG Programming Examples

## Introduction

Most of the HP 20s features can be used in Program mode just like they are used manually. Following are two sample programs for the HP 20s.

## Calculator symbol key

The procedures in this document use the following text to represent symbol keys:
 Key Description Text Representation Right shift key RS Left-shift key LS Square root key SQx

## Pythagorean theorem

To illustrate how STO and RCL are used to recall data from registers in a program, enter the following Pythagorean theorem program. It calculates the length of the hypotenuse (side c) of a right triangle, given the lengths of sides a and b. The formula used is in Figure 1. Assume that the calculation begins with side a in R1 and side b in R2.
Figure : Pythagorean formula Keys Display Description Press LS, then PRGM Enters Program mode. Press RS, then CLPRGM 00- Clears program memory. (Skip this step to leave programs intact.) Press RS, LBL, then E 01- 61 41 E Labels program "E". Press RCL, then 1 02- 22 1 Recalls a from R1 Press LS, then 2 03- 51 11 a2 Press + 04- 75 Press RCL, then 2 05- 22 2 Recalls b from R2. Press LS, then x2 06- 51 11 b2 Press = 07- 74 a2+ b2 Press SQx 08- 11 Press RS, then RTN 09- 61 26 Hold LS and SHOW 3902 Checksum (see Checksum below) Press LS, then PRGM Exits Program mode.
Now store the a and b values of 22 and 9 into R1 and R2, then run the program:
 Keys Display Description Press 22, STO, then 1 22.0000 Stores a in R2 Press 9, STO, then 2 9.0000 Stores b in R2 Press XEQ, then E 23.7697 Length of the hypotenuse.

## Random number generator

The following program generates random numbers in the range 0 < r1< 1. The program uses a starting value between 0 and 1. For a different sequence of random numbers, use a different starting value.
note:
The program uses the algorithm: r1+ 1 = FP (997rit>), where r0 is a starting value between 0 and 1 (for example, 0.5284163). The random number generator passes the chi-square frequency tests for uniformity, and the serial and run tests for randomness. The most significant digits are more random than the least significant digits. If the starting value is between 0 and 1, and if the starting value x 107 is not divisible by 2 or 5, then the generator produces 500,000 different random numbers before repeating.
 Keys Display Description Press LS, then PRGM Enters Program mode. Press RS, then CLPRGM 00- Clears program memory. (Skip this step to leave programs intact.) Press RS, LBL, then A 01- 61 41 A Names program "A". Press RCL, then 0 02- 22 0 Get rI. Press x 03- 55 Multiplies ... Press 9 04- 9 Press 9 again 05- 9 Press 7 06- 7 ... by 997 Press = 07- 74 Equals 997. rI Press RS, then FP 08- 61 45 rI+ 1 = FP (997rI) Press STO, then 0 09- 21 0 Saves rI + 1 Press RS, then RTN 10- 61 26 Ends program Hold LS and SHOW 7Ab8 Checksum (see Checksum below) Press LS, then PRGM Exits Program mode.
To store the starting value in R0 and run the program:
 Keys Display Description Press .5284163 0.5284163_ Enters starting value in display. Press STO, then 0 0.5284 Stores starting value in R0 Press XEQ, then A 0.8311 Generates first random number. Press XEQ, then A again. 0.5579 Generates second random number.
Continue pressing XEQ, then A to continue generating random numbers.
To scale the random numbers to within the range lower limit < Riupper limit, add program lines to multiply the random number by the difference between the limits, and add the product to the lower limit. That is:
scaled Ri = (upper limit - lower limit)ri+ lower limit

## Checksum

After a program is entered, check to see if the keystrokes are entered correctly by comparing the checksum listed in this document to the checksum created by the program. The checksum is a unique hexadecimal value assigned to the specific keystrokes entered. To view the checksum, press and hold LS and SHOW for a moment while in Program mode.

Country/Region: United States