Thursday, October 6, 2016

HP 12C Programming Tricks



HP 12C Programming Tricks


These tricks can help you cut steps in your programming.  Remember that it is still important to test results.  Happy programming!

Using Last X

This can be a god-send in making programs more efficient.  Unless you are using the Platinum Edition, we only have 99 steps to work with.  The LST X function returns the x argument that is last used in a calculation.

Function
LST x Returns
One-Argument Functions:
√, LN, e^x, FRAC, INTG, n!
The x pre-calculation
Two-Argument Functions (Arithmetic):
+, -, *, ÷, y^x
The x value in the arithmetic operation

Number of Inputs

I follow a general rule when it comes to the number of inputs required on an HP 12C program.  If the number of inputs is 1 or 2, I would have the user enter inputs on the stack and run the program.  If the number of inputs is 3 or more, I have the user pre-store the inputs into registers before pressing [R/S] to run the program.  Example:  If my program calls for a, b, and c, I have the user store a in register 0, b in register 1, and c in register 2, and then have the user press [R/S].

Branching

You can branch a program to different parts of the program.  You designate a register as a choice variable.  (Rc = 0 for option 0, 1 for option 1, etc).  Examples include setting the variable for computing combinations vs permutations, set for month payments vs yearly payments.


Program format:

RCL Rc
Test value
-
X=0?
GTO (applicable line number)
(applicable section)
(commands and calculations)
GTO 00 (end the program)

Multiply x by 100

Keystroke:  [ENTER], 1, [x<>y], [%T]
Result:  The X stack has 100*x, the Y stack has x.

Dividing x by 100

Keystrokes: [ENTER], 1, [ % ]
Results:  The x stack has x/100, the Y stack has x.   

Quickly entering numbers in the form of 10^n (n is an integer)

Keystrokes:  1, [EEX], n
This is useful when n>2.  For example, entering 10000 takes five steps normally.  With this sequence, 1 [EEX] 5, you only use 3. 

Another Way to Double

Keystrokes:  [ENTER], [ + ]
Results: X Stack:  2*x.   1 program step saved.

Absolute Value of x.

Keystrokes:  2, [y^x], [ g ] [ √ ]
Result: X Stack: |x|.  It is important to square the number first since the HP 12C’s square root function returns an error on negative numbers. 

Signum Function of x.
This is the extension of the absolute value sequence shown previously.

Keystrokes:  [ENTER], 2, [y^x], [ g ] ( √ ), [ ÷ ]
Result:  X Stack: sgn(x)

Modulus (for two positive values)

Keystrokes:  a, [ENTER], b, [ ÷ ], [ g ] (LSTx), [x<>y], [ g ] (FRAC), [ * ]
Result:  X stack:  a MOD b

Comparing Values

We have a value stored in a register, call it Rc, where c obviously can stand for 0, 1, and so on. 
Keystrokes: [RCL], c, [ - ], [ g ] (x=0)

If the test is true (x = Rc), the next step is executed.  Otherwise, it is skipped.

Six Digit Approximation of π

Since the HP 12C does not have a π button, we’ll have to enter a 10 digit approximation of π, which will take 11 steps.  However, if you want an approximation (given most of the time the HP 12C is used on Fix 2 mode), we can use the approximation π ≈ 355/113, which is accurate to six decimal places.

π ≈ 3.14159265359
355/113 ≈ 3.14159292035

Keystrokes:  355, [ENTER], 113, [ ÷ ]

Calculate (1 + n/100)^x without using the TVM Registers

Keystrokes:  1, [ENTER], n, [ % ], [ + ], x, [y^x]

I hope you find these tricks useful.  If you have any, please feel free to share them. 

Eddie

This blog is property of Edward Shore, 2016.