Saturday, July 9, 2016

HP 42S Programming Part I: Matrix Column Sum, GCD, Error Function

HP 42S Programming Part I:  Matrix Column Sum, GCD, Error Function

Continuing with calculators from the 1980s, this series will present one of the most beloved scientific calculators and one of the most sought after: the Hewlett Packard HP 42S from the late 1980s.  If you want one, you might want to save up because HP 42S calculators are valuable, and often cost over $100.

It is one of my most favorite calculators of all time. 

Free42

However, you can use an emulator for the HP 42S for free, check out the Free42 by Thomas Oakken.  It is a rewrite of the HP 42S code and the emulator is available on many platforms including Widows, Mac, iOS, and Android.  Link:  http://thomasokken.com/free42/   

Oakken accepts donations.





Part III hopefully to come next week.  

As usual, anything that comes after the double-backslash is a comment only.

HP 42S: Column Sums of a Matrix

This could potentially had been a medium to long sized programs involving loops, but thanks to the powerful function RSUM (found in CATALOG-FUNC or executed by XEQ RSUM) this program will be quite short.  RSUM is the row sum function, or the sum of each row of a matrix.

Just put a matrix on the stack and run CSUM.  When the program is finished, press [shift] [ 9 ] (MATRIX), {EDIT} to see the sums.

00 {12-Byte Prgm}
01 LBL “CSUM”
02 TRANS \\ transpose
03 RSUM
04 END

Test:  MAT1 = [ [4, 3, 1], [5, 8, 2], [7, 6, 3]]
Result:  [16, 17, 6]

HP 42S:  GCD (Greatest Common Divisor) by Euclid Division

Enter the two integers on the Y stack and X stack.  Order doesn’t matter.

00 {42-Byte Prgm}
01 LBL “GCD”
02 X<Y?
03 X<>Y
04 STO 01  \\ store maximum in R01
05 X<>Y
06 STO 00  \\ store minimum in R00
07 LBL 00 \\ main loop
08 RCL 01
09 ENTER
10 RCL÷ 00
11 IP
12 RCL* 00
13 –
14 X=0?  \\ is max – IP(min/max)*min = 0?
15 GTO 01
16 X<>Y
17 STO 01
18 X<>Y
19 STO 00
20 GTO 00
21 LBL 01 \\ display GCD
22 RCL 00
23 “GCD:”
24 ARCL ST X
25 AVIEW
26 END


Test:   Find the GCD of 485 and 175.
485 [ENTER] 175 [XEQ] {GCD}   Result:  5
Alternatively:
175 [ENTER] 485 [XEQ] {GCD}   Result:  5


HP 42S:  Error (erf) Function


ERF(X) = ∫ 2/√π * (e^(-T^2)) dT, from T = 0 to T = x

Integrand:
00 {22-Byte Prgm}
01 LBL “ERF”
02 MVAR “T”
03 RCL “T”
04 X^2
05 +/-
06 E^X
07 2
08 *
09 PI
10 SQRT
11 ÷
12 END

Instructions:

Press [(shift)] [ 8 ] (∫ f(x) )
Choose ERF at the “Select ∫f(x) Program”
Select T at Set Vars; Select ∫var
Store 0 in LLIM, X in ULIM, a suitable accuracy factor (10^-n) in ACC
Press the ∫ soft key.

Test: erf(1.2) with ACC of 1E-6  (LLIM = 0, ULIM = 1.2) 
Result:  0.9103


 This blog is property of Edward Shore, 2016.