Showing posts with label Euclid Algorithm. Show all posts
Showing posts with label Euclid Algorithm. Show all posts

Sunday, November 20, 2022

HP 32SII: Some Algorithms For RPN Calculators

HP 32SII:  Some Algorithms For RPN Calculators



Four programs ported to the HP 32SII calculator from algorithms designated for the 1973 HP 45 calculator.  



HP 32SII: Euclid Algorithm - Greatest Common Divisor (GCD)


The HP 45 algorithm is found on page 228 in the Algorithms For RPN Calculators book. (see source below)


This algorithm takes up three labels.


E01 LBL E

E02 INPUT M

E03 ENTER

E04 ENTER

E05 ENTER

E06 INPUT N

E07 x<>y


K01 LBL 1

K02 ÷

K03 FP

K04 ×

K05 1

K06 x>y?

K07 GTO L

K08 R↓

K09 ENTER

K10 ENTER

K11 R↓

K12 R↓

K13 GTO K


L01 LBL L

L02 R↓

L03 R↓

L04 RTN


Sizes and Checksums:

E:  10.5 bytes, 9D4D

K:  19.5 bytes, F8AD

L:  6.0 bytes, C304

Total:  36.0 bytes


Instructions:

Press [ XEQ ] E, enter M and N.   


Examples:


Input:  M = 36, N = 28.  Result:  4

Input:  M = 48, N = 126. Result: 6

Input:  M = 115, N = 300.  Result: 5


HP 32SII:  GCD Using One Label - John Kenney 


The program was provided by Ross Barnes, and the algorithm is from the book ENTER by J. Daniel Dodlin and Keith Jarrett (ISBN 0-9615174-2-1, pg. 84).  This is smart, one label program.


Enter both numbers in the stack before running the program.


G01 LBL G

G02 ENTER

G03 ENTER

G04 -

G05 R↓

G06 x<>y

G07 LASTx

G08 /

G09 LASTx

G10 RDN

G11 IP

G12 x

G13 -

G14 x≠0?

G15 GTO G

G16 +

G17 RTN


Size and Checksum:  25.5 bytes, 4E39


Posted with permission.  


HP 32SII:  Tetens Equation


The HP 45 algorithm is found on page 290 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Celsius. 


Find the saturation of water vapor (e_m) in mmHg (millimeters of Mercury) given the temperature in °F.


Determined Formulas:

T (in °C) = (T°F - 32) * 5/9

α = T/(236.87 + T)

e_m = 4.579 * 10^(7.49 * α)


T01 LBL T

T02 INPUT T

T03 →°C

T04 ENTER

T05 ENTER

T06 236.87

T07 +

T08 ÷

T09 7.49

T10 ×

T11 10^x

T12 4.579

T13 ×

T14 RTN


Size and Checksum:

45.0 bytes, 404A


Examples:

T = 68 °F, Result: 17.53658 mmHg

T = 99 °F, Result:  47.63501 mmHg




HP 32SII:  Dew Point Given Relative Humidity and Air Temperature


The HP 45 algorithm is found on page 290 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Celsius. 


Relativity humidity (F) is to be entered as a decimal.  For instance, instead of 20%, enter 0.20.


Determined Formulas:

T (in °C) = (T°F - 32) * 5/9

A = T/(T + 236.87)

B = 1/(log F/7.49 + A)

TD = 236.87/(B - 1)

TD = TD * 9/5 + 32


D01 LBL D

D02 INPUT T

D03 →°C

D04 ENTER

D05 ENTER

D06 236.87

D07 STO A

D08 +

D09 ÷

D10 INPUT F

D11 LOG

D12 7.49

D13 ÷

D14 +

D15 1/x

D16 1

D17 -

D18 RCL÷ A

D19 1/x

D20 →°F

D21 RTN


Size and Checksum:

47.5 bytes, 8677


Examples:

T = 80, F = 0.64, Result:  66.725

T = 95, F = 0.32, Result:  60.50684



HP 32SII:  Effective Temperature Due to Wind Velocity



The HP 45 algorithm is found on page 291 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Fahrenheit. 


Wind velocity is in miles per hour (mph).  


Determined Formulas:

A = 0.634*(0.634 - log V)

ΔT = A*(T - 90)

Effective T = T - ΔT


E01 LBL E

E02 0.634

E03 ENTER

E04 ENTER

E05 INPUT V

E06 LOG

E07 -

E08 ×

E09 INPUT T

E10 ENTER

E11 90

E12 -

E13 ×

E14 RCL T

E15 x<>y

E16 -

E17 RTN


Size and Checksum:

33.5 bytes, 54F7


Examples:

V = 20 mph, T = 15 °F,  Result: -16.71728

V = 15 mph, T = 86 °F,  Result: 84.62526


Source:


Ball, John A.  Algorithms For RPN Calculators  John Wiley & Sons:  New York, NY.  1978. ISBN 0-471-03070-8

For the second GCD program:

Dodin, J. Daniel and Keith Jarrett. ENTER: Reverse Polish Notation Made Easy   Synthetix:  Berkeley, CA   ISBN 0-9612174-2-1  1984.


Special thanks and gratitude to Ross Barnes.  



All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, October 16, 2021

Swiss Micros DM41X: Applications

Swiss Micros DM41X: Applications





Swiss Micros DM41X Program:  Euclid Algorithm


Registers:

R01:  A

R02: B

R03:  C  (used)


Finds the GCD of A and B, where A and B are positive integers and A > B.


01 LBL^T EUCLID

02 ^T GCD A>B

03 AVIEW

04 PSE

05 ^T A?

06 PROMPT

07 STO 01

08 ^T B?

09 PROMPT

10 STO 02

11 LBL 00

12 RCL 01

13 ENTER

14 ENTER

15 RCL 02

16 /

17 LASTX

18 X<>Y

19 INT

20 *

21 - 

22 STO 03

23 X=0?

24 GTO 01

25 RCL 02

26 STO 01

27 X<>Y

28 STO 02

29 GTO 00

30 LBL 01

31 ^T GCD=

32 ARCL 02

33 AVIEW

34 END


Examples:


A = 100, B = 20, GCD = 20

A = 78, B =24, GCD = 6


Swiss Micros DM41X Program: Fan Laws 


The program will calculate RPM_new (Revolutions per Minute), SP_new (Static Pressure), and BHP_new (Brake Horsepower).  


Inputs:

CFM_old:  Cubic Feet of Minute - old

CFM_new:  Cubic Feet of Minute - new

RPM_old:  Revolutions per Minute - old

SP_old:  Static Pressure - old

BHP_old:  Brake Horsepower - old


Outputs:

RPM_new

SP_new

BHP_new


01 LBL^T FANLAWS

02 CLA

03 ^T CFM.OLD?

04 PROMPT

05 STO 01

06 ^T CFM.NEW?

07 PROMPT

08 STO 02

09 X<>Y

10 /

11 STO 04

12 STO 06

13 ST* 06

14 STO 08

15 ST* 08

16 ST* 08

17 ^T RPM.OLD?

18 PROMPT

19 STO 03

20 ST* 04

21 ^T SP.OLD

22 PROMPT

23 STO 05

24 ST* 06

25 ^T BHP.OLD?

26 PROMPT

27 STO 07

28 ST* 08

29 ^T RPM.NEW=

30 ARCL 04

31 AVIEW

32 STOP

33 ^T SP.NEW=

34 ARCL 06

35 AVIEW

36 STOP

37 ^T BHP.NEW=

38 ARCL 08

39 AVIEW

40 END


Variables:


R01 = CFM.OLD

R02 = CFM.NEW

R03 = RPM.OLD

R04 = RPM.NEW

R05 = SP.OLD

R06 = SP.NEW

R07 = BHP.OLD

R08 = BHP.NEW


The program uses a lot of storage arithmetic.  


Example


Inputs:

CFM.OLD:  1250 CFM

CFM.NEW: 1600 CFM

RPM.OLD:  840 RPM

SP.OLD: 4 in

BHP.OLD: 7 BHP


Results:

RPM.NEW: 1075.2 RPM

SP.NEW: 6.5536 in

BHP.OLD:  14.680064 BHP


Source:

Calculated Industries "Sheet Metal/HVAC Pro Calc User's Guide" 2021


Swiss Micros DM41X Program: Johnson-Nyquist Noise Analysis


Equations Used:


Power (in Watts):


P = kb * T * Δf


RMS Voltage (in Volts):


v_n = √(4 * R * kb * T * Δf) = √(4 * R * P)


Current (in Amps):


i_n = √((4 * T * kb * Δf / R) = v_n / R


Inputs:


T = temperature in Kelvin  (°C + 273.15)

Δf = bandwidth, difference of frequencies in Hz

R = resistance in ohms (Ω)


Constants:  Boltzmann's Constant

kb ≈ 1.380649 * 10^-23 J/K


Program:


01 LBL^T NOISE

02 ^T TEMP? <K>

03 PROMPT

04 ^T BANDWIDTH?

05 PROMPT

06 *

07 1.308649E-23

08 *

09 ^T POW=

10 ARCL X

11 AVIEW

12 STOP

13 ^T R?

14 PROMPT

15 *

16 LASTX

17 X<>Y

18 4

19 *

20 SQRT

21 ^T V=

22 ARCL X

23 AVIEW

24 STOP

25 X<>Y

26 /

27 ^T I=

28 ARCL X

29 AVIEW

30 END


Example:


Temperature:  299.68 K

Bandwidth:  10,500 Hz

Resistance:  1375 Ω


Results:


Power:  4.3444E-17 W

Volts:  4.8882E-7 V

Current:  3.5550E-10 A


"Johnson-Nyquist Noise" Wikipedia.  Retrieved February 15, 2015 https://en.wikipedia.org/wiki/Johnson%E2%80%93Nyquist_noise




Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Monday, January 16, 2017

Fun with the Sharp EL-5500 III

Fun with the Sharp EL-5500 III


Even though the EL-5500 III has one programming space, we can fit many programs in it.  One of the great features is that how programs take relatively little space.  Obviously you can change the line numbers, the sequence of lines are most important.

Euclid Algorithm – RUN 10

10 PAUSE “GCD: A>B”
18 INPUT “A: “; A, “B: “; B
20 C = A – INT(A/B)*B
30 IF C = 0 THEN 40
32 A = B
34 B = C
36 GOTO 20
40 PRINT “GCD = “; B
46 END

My first program on the EL-5500 III.  We can probably make the coding more efficient.  PRINT with a proceeding WAIT pauses execution until [ ENTER ] is pressed.

The command END ends the program execution and allows the space to have more than one routines.

Examples:
A: 100, B: 20.  Result:  GCD = 20
A: 63, B: 60.  Result:  GCD = 3

Binomial Expansion – RUN 50

50 PAUSE “(Ax + B)^n”
55 INPUT “A: “;A, “B: “;B, “N: “,N
60 FOR I=0 TO N
65 C = FACT(N) / (FACT(I) * FACT(N-I))
70 T = C * A^(N-I) * B^I
75 PRINT T; “*x^”; N-I
80 NEXT I
85 END

The command PAUSE shows the text for about 0.85 seconds.

This program displays each coefficient one at time.

Example:  A = 3, B = -2, N = 3.  Expand (3 – 2x)^3.
Result:  27, -54, 36, -8  (27x^3 – 54x^2 + 36x – 8)

Days Between Dates – RUN 100

100 PAUSE “Days between Dates”
112 INPUT “M1:”; M1, “D1:”; D1, “Y1:”; Y1
113 INPUT “M2:”; M2, “D2:”; D2, “Y2:”; Y2
115 M = M1: D = D1: Y = Y1
117 GOSUB 140
119 F1 = F
121 M = M2: D = D2: Y = Y2
123 GOSUB 140
125 F2 = F
127 N = F2 – F1
129 PRINT “# DAYS: “; N
131 END
140 IF M > 2 THEN 150
142 X = 0: Z = Y – 1
144 GOTO 160
150 X = INT(.4 * M + 2.3): Z = Y
160 F = 365 * Y + 31* (M – 1) + D + INT(Z/4) – X
165 RETURN

If I had it my way, every calculator that isn’t a basic 4-function calculator will have a Days Between Dates function.

Example:  November 4, 1986 to January 12, 2017
M1:  11, D1: 4, Y1: 1986 
M2: 1, D2: 12, Y2: 2017
Result:  11,027

Power of a Complex Number – RUN 200

This calculates (a + bi)^n

200 PAUSE “(A + Bi)^n”
203 INPUT “A: “;A, “B: “;B, “n: “;N
205 IF A<>0 THEN 210
207 A = A^N: GOTO 220
210 R = √(A^2 + B^2)^N
212 T = ATN(B/A)*N
214 A = R * COS T
216 B = R * SIN T
220 PRINT A; “+”; B; “i”
225 END

The command <> means not equals (≠) and ATN is the arctangent function.

Examples:
(2 + 3i)^3:  A = 2, Bi = 3.  Result: -46 + 9i
(-5 + i)^2:  A = -5, Bi = 1.  Result: 24 – 10i

Error Function Approximation – RUN 250

250 PAUSE “APPROX ERF(X)”
252 CLEAR
254 DIM A(3)
256 E = 0
260 INPUT “X > 0 : “; X
262 T = RCP(1 + .47047 * X)
270 FOR I = 1 TO 3
272 READ A(I)
274 E = E + A(I) * T^I
276 NEXT I
280 DATA .3480242, -.0958798, .7478556
290 E = 1 – E * EXP(-(X^2))
292 PRINT “ERF : “, F
294 CLEAR
296 END

Use the command CLEAR to clear all the variables.  At this point, I will use this command both at the beginning and end of calculations.  The command RCP is the reciprocal.  Finally, EXP is from e^x function, not the [EXP] key.

Example:
x = 0.53, erf ≈ 0.546455764
x = 0.88, erf ≈ 7.86708E-01 = 0.786708

Keep in mind this approximation is accurate 2.5 parts in 10^-5.

Source:  Smith, Jon M.  Scientific Analysis on the Pocket Calculator  John Wiley & Sons: New York. 1975.  ISBN 0-471-79997-1

Simpson’s Rule – RUN 300

Calculates the numeric integral ∫ f(x) dx from x = L to x = U.

You can edit the function at line 342.  In BASIC-PRO mode, call up line 342 by LIST 342.  Edit as desired.

300 PAUSE “Simpsons Rule”
302 CLEAR
304 INPUT “LOWER: “; L, “UPPER: “; U, “PARTS (even): “; N
306 RADIAN
308 X = L: GOSUB 340: T = F
310 X = U: GOSUB 340: T = T + F
314 H = (U – L)/N
320 FOR I = 1 TO N-1
322 X = L + I * H: GOSUB 340
324 R = I/2 – INT(I/2)
326 IF R = 0 THEN LET T = T + 2*F
328 IF R <> 0 THEN LET T = T + 4*F
330 NEXT I
332 T = T * H/3
334 PRINT “Integral: “, T
336 CLEAR
338 END
340 REM f(x)
342 F = [insert f(X) here]
344 RETURN

Note:  I finally learn why LET is included in BASIC.  In an IF-THEN statement, if you want to assign a value or calculation to a variable, you will need a LET command.

I use a REM (remark) command on line 340.  REM is for comments and nothing said after REM is executed.

Examples:

342 F = X^2 – 1
L = -2, U = 2, N = 14.  Result:  1.333333334 (Actual: 4/3 ≈ 1.333333333)

342 F = √(X – 1)
L = 2, U = 3, N = 14.  Result: 1.218951372 (Actual:  ≈ 1.2158951416)

Dancing Star Demo – RUN 400

400 PAUSE “Dancing Star Demo”
405 CLEAR
410 S$ = “         “  (9 spaces)
415 FOR I = 1 TO 48
420 N = RND 7 + 1
425 T$ = MID$(S$, 1, N-1) + “*” + MID$(S$, N+1, 9-(N+1))
430 WAIT 15: PRINT T$
440 NEXT I
445 BEEP 3: CLEAR: END

This is just show a dancing asterisk (*) on the screen.

WAIT 15: PRINT T$:   This causes the string T$ after a quarter of a second.  WAIT operates in 1/60 seconds.

RND n:  This is the random command.  If n = 1, the result is between 0 and 1. 

BEEP n:  This makes the EL-5500 III beep n times. 

Eddie

This blog is property of Edward Shore, 2017.



Thursday, June 30, 2016

Fun With the HP 71B III

Fun With the HP 71B III


3x3 Matrices: Determinant, Inverse, 3x3 Linear Systems
EWS 6/29/2016

The program MATX3 calculates:

1. The determinant and (if possible), the inverse of a 3x3 matrix M.
2. The solution to a 3x3 linear system: Mq=D. The determinant of M will also be displayed.

If det(M) = 0, then the matrix is singular and execution stops.

The matrix M is broken into three columns (3x1 arrays): [ M ] = [ A | B | C ].

Hence M = [[ A1 B1 C1 ] [ A2 B2 C2 ] [ A3 B3 C3 ]]

Other variables used:
E = det(M)
I = M^-1. Unlike M, I will be a 3 x 3 array.
R, K, S, H: other variables used

Program MATX3 (767 bytes)
10 DESTROY A,B,I,C,R,K,S,H,D,Q
11 DISP “1. DET/INV  2. 3x3” @ WAIT 2
12 INPUT “1. D/I 2. SYS:”; H
13 DIM A(3),B(3),C(3),I(3,3),D(3)
14 OPTION BASE 1
20 FOR K=1 TO 3
21 DISP “ROW “;K @ WAIT 1
22 INPUT “A:”; A(K)
24 INPUT “B:”; B(K)
26 INPUT “C:”; C(K)
28 IF H=2 THEN INPUT “D:”; D(K)
30 NEXT K
40 DEF FND(X,Y,Z,T)=X*T-Y*Z
42 E=A(1)*FND(B(2),C(2),B(3),C(3))
44 E=E-B(1)*FND(A(2),C(2),A(3),C(3))
46 E=E+C(1)*FND(A(2),B(2),A(3),B(3))
50 DISP “DET:”; E
52 IF E=0 THEN STOP
60 I(1,1)=FND(B(2),B(3),C(2),C(3))/E
62 I(1,2)=-FND(B(1),B(3),C(1),C(3))/E
64 I(1,3)=FND(B(1),B(2),C(1),C(2))/E
66 I(2,1)=-FND(A(2),A(3),C(2),C(3))/E
68 I(2,2)=FND(A(1),A(3),C(1),C(3))/E
70 I(2,3)=-FND(A(1),A(2),C(1),C(2))/E
72 I(3,1)=FND(A(2),A(3),B(2),B(3))/E
74 I(3,2)=-FND(A(1),A(3),B(1),B(3))/E
76 I(3,3)=FND(A(1),A(2),B(1),B(2))/E
78 IF H=2 THEN 100
80 FOR R=1 TO 3
82 FOR S=1 TO 3
84 DISP “I(“; R; “,”; S; “):”; I(R,S)
86 PAUSE
88 NEXT S
90 NEXT R
92 STOP
100 DIM Q(3)
102 FOR K=1 TO 3
104 Q(K)= I(K,1)*D(1) + I(K,2)*D(2) + I(K,3)*D(3)
106 DISP “Q”; K; “:”; Q(K) @ PAUSE
108 NEXT K

Example:

M = [[ 1, 2, -8 ] [ 0, -2, 9.5 ] [ 3.2, 2.7, -1 ]]
D = [[ 0.5 ] [ 1.5 ] [ 2.5 ]]

DET = -14.05
I ≈ [[ 1.6833, 1.3950, -0.2135 ] [ -2.1637, -1.7509, 0.6762 ] [ -0.4555, -0.2633, 0.1423 ]]

Solutions:
Q ≈ [[ 2.4004 ] [ -2.0178 ] [ -0.2669 ]]

Days From January 1, Days Between Dates
HP 71B – Day Counts

Number of Days from January 1

D = Day
M = Month
L = Leap Year indicator (1 if the year is a leap year, 0 if it is not)

Program DAYJAN1 (183 bytes)
10 DESTROY D,M,L,N
12 INPUT “MONTH:”; M
14 INPUT “DAY:”; D
16 INPUT “LEAP? (Y=1,N=0):”; L
20 IF M>2 THEN 28
21 REM M<=2
22 N=IP(30.6*(M+13))+D-429
24 GOTO 32
27 REM M>2
28 N=IP(30.6*(M+1))+D+L-64
32 DISP “# DAYS:”; N

Test 1: January 1 to May 29, non-leap year (L=0). Result: 148
Test 2: January 1 to May 29, leap year (L=1). Result: 149

Days between Dates

M, D, Y: Month, Day, four-digit year

Program DDAYS (299 bytes)
10 DESTROY M1, M2, D1, D2, Y1, Y2, F1, F2
11 DESTROY F,M,D,Y,N,X,Z
12 INPUT “1: M,D,Y:”; M1, D1, Y1
13 INPUT “2: M,D,Y:”; M2, D2, Y2
15 M=M1 @ D=D1 @ Y=Y1
17 GOSUB 40
19 F1=F
21 M=M2 @ D=D2 @ Y=Y2
23 GOSUB 40
25 F2=F
27 N=F2-F1
29 DISP “# DAYS:”; N
31 STOP
40 IF M>2 THEN X=IP(.4*M+2.3) ELSE X=0
42 IF M>2 THEN Z=Y ELSE Z=Y-1
44 F=365*Y+31*(M-1)+D+IP(Z/4)-X
46 RETURN

Test 1: January 2, 2015 to March 17, 2016 (1,2,2015 to 3,17,2016). Result: 440
Test 2: March 14, 1977 to June 29, 2016 (3,14,1977 to 6,29,2016). Result: 14352

Great Circle Distance

N: Longitude
E: Latitude

Separate Degrees (H), Minutes (M), and Seconds (S) during input
Program GRCIC (367 bytes)
10 DESTROY D,M,H,S
11 DESTROY N1,N2,E1,E2,G
12 DEGREES
14 DISP “N: LATITUDE” @ WAIT 1
16 DISP “E: LONGITUDE” @ WAIT 1
20 INPUT “N1: D,M,S:”; H,M,S
21 GOSUB 50
22 N1=D
25 INPUT “E1: D,M,S:”; H,M,S
26 GOSUB 50
27 E1=D
30 INPUT “N2: D,M,S:”; H,M,S
31 GOSUB 50
32 N2=D
35 INPUT “E2: D,M,S:”; H,M,S
36 GOSUB 50
37 E2=D
40 REM CALCULATION
41 G=SIN(N1)*SIN(N2)+COS(N1)*COS(N2)*COS(E1-E2)
42 G=ACOS(G)*3959*PI/180
44 DISP “DIST: “; G; “ MI”
46 STOP
50 D=SGN(H)*(ABS(H)+M/60+S/3600)
52 RETURN

Test:
Los Angeles, N = 34°13’0” and E = -118°15’0”
San Francisco, N = 37°47’0” and E = -112°25’0”
Distance ≈ 408.5961 mi

Euclid Division – Finding the GCD

Finds the GCD (greatest common divisor) between integers M and N.  The program displays a “calculating” screen while the calculation is in process.

Program EUCLID (143 bytes)
10 DESTROY M,N,A,B,C
15 INPUT “M,N:”; M,N
20 IF M>N THEN A=M  @ B=N
22 IF M<N THEN A=N @ B=M
30 C= A – IP(A/B)*B
35 DISP C;B;A   // this is the “busy” indicator
40 IF C=0 THEN 50
45 A=B @ B=C @ GOTO 30
50 DISP “GCD:”; B

Test 1:  M=144, N=14;  Result: 2
Test 2:  N=14, M=144;  Result: 2

Fractions:  Addition and Multiplication

Adds or multiplies two fractions W/X and Y/Z.  Gives the result in the simplest form.  Proper or improper fractions only.

Separate each part with a comma (W,X,Y,Z) as prompted.

Program FRAC (380 bytes)
10 DESTROY W,X,Y,Z,N,D,H,A,B,C
20 INPUT “1. + 2. *:”,H
24 ON H GOTO 41,51
41 REM ADD
42 INPUT “W/X+Y/Z:”; W,X,Y,Z
44 N=W*Z+X*Y @ D=X*Y
46 GOSUB 61
48 DISP “=”; N; “/”; D @ STOP
51 REM MULT
52 INPUT “W/X*Y/Z:”; W
54 N=W*Y @ D=X*Z
56 GOSUB 61
58 DISP “=”; N; “/”; D @ STOP
61 REM SIMPLIFY
62 IF N>D THEN A=N @ B=D
64 IF D>N THEN A=D @ B=N
66 IF D=N THEN N=1 @ D=1 @ RETURN
68 C= A – IP(A/B)*B
69 DISP C;B;A
70 IF C=0 THEN 74
72 A=B @ B=C @ GOTO 68
74 N=N/B @ D=D/B @ RETURN

Test 1:  4/7 + 3/13;   Input:  4,7,3,13.  Result:  73/91
Test 2: 4/7 * 3/13; Input: 4,7,3,13.  Result:  12/91

Statistics: Regression

The program CURVEFIT fits data to one of five regression models:

1.  Linear Regression:  y = a + bx
2.  Exponential Regression:  y = a * e^(b*x)
3.  Logarithm Regression: y = a + b * ln x
4.  Power Regression:  y = a * x^b
5.  Inverse Regression:  y = a + b/x

On the HP 71, the ln function is represented by LOG.

The program will allow different calculations with the same data set.

Program CURVEFIT (622 bytes)
10 DESTROY H,S,D,X,Y,A,B,E
12 STAT S(2) @ CLSTAT
20 REM CHOOSE REG
22 DISP “1. LIN 2. EXP 3. LOG” @ WAIT 1.5
24 DISP “4. POW 5. INV” @ WAIT 1.5
28 INPUT “CHOICE #:”; H
29 IF E=1 THEN 60
30 REM INPUT PROCESS
32 INPUT “X,Y:”; X,Y
34 ON H GOTO 36,38,40,42,44
36 ADD X,Y @ GOTO 46
38 ADD X,LOG(Y) @ GOTO 46
40 ADD LOG(X),Y @ GOTO 46
42 ADD LOG(X),LOG(Y) @ GOTO 46
44 ADD 1/X,Y @ GOTO 46
46 INPUT “DONE? (Y=1,N=0):”; D
48 IF D=0 THEN 32
60 LR 2,1,A,B
62 IF H=2 OR H=4 THEN A=EXP(A)
64 ON H GOTO 70,72,74,76,78
70 DISP A; “+”; B; “x” @ GOTO 80
72 DISP A; “*EXP(“; B; “x)” @ GOTO 80
74 DISP A; “+”; B; “*LOG(x)” @ GOTO 80
76 DISP A; “x^”; B @ GOTO 80
78 DISP A; “+”; B; “/x” @ GOTO 80
80 PAUSE
84 DISP “ANOTHER ANALYSIS?” @ WAIT 1.5
86 INPUT “Y=1,N=0:”; E
88 IF E=1 THEN 20
90 DISP “DONE”


 Have fun!  See you in the second half of 2016!  

Eddie

This blog is property of Edward Shore, 2016

Monday, December 3, 2012

Numeric CAS Part 6: Euclid Algorithm to find GCD (Greatest Common Divisor)

Euclid Algorithm

Goal: To find the greatest common divisor of two integers U and V.

The Algorithm in Brief:

Let U ≥ V. Then U = A * V + R where A is the quotient and R is the remainder. If R≠0, then V becomes the new U, and R becomes the new V. The process repeats until you R=0. When that happens, V_last = GCD(U,V).

The program allows for you two enter integers in any order. Each program uses the home screen to display the steps, but the HP 39gii displays all the steps on one screen.

Example: gcd(166, 78)

166 = 2 * 78 + 10
78 = 7 * 10 + 8
10 = 1 * 8 + 2
8 = 4 * 2 + 0
GCD = 2

gcd(169,39)

169 = 4 * 39 + 13
39 = 3 * 13 + 0
GCD = 13


Casio Prizm:

EUCLID
GCD Program - 12/3/12
212 bytes

"X"?→ X
"Y"?→ Y
If X ≥ Y
Then X → M
Y → N
Else X → N
Y → M
IfEnd
Lbl 1
Int(M ÷ N) → A
M - A × N → R
ClrText
Locate 1,2,M
Locate 1,3,"="
Locate 2,3,A
Locate 13,3,"×"
Locate 14,3,N
Locate 1,4,"+"
Locate 2,4,R ◢
If R=0
Then Goto 2
Else N → M
R → N
Goto 1
IfEnd
Lbl 2
Locate 1,6,"GCD="
Locate 6,6,N


TI-84+:

GCD by Euclidian Algorithm
11/25/2012 - 160 bytes

Program EUCLID

Prompt X,Y
min(X,Y) → N
max(X,Y) → M
Lbl 1
iPart(M/N) → A
M - A * N → R
ClrHome
Output(2,1,M)
Output(3,1,"=")
Output(3,2,A)
Output(3,8,"*")
Output(3,9,N)
Output(4,1,"+")
Output(4,2,R)
Pause
If R=0
Then
Goto 2
Else
N → M
R → N
Goto 1
End
Lbl 2
ClrHome
Disp "GCD=", N


HP 39gii:

Program EUCLID
Finding the GCD by the Euclid algorithm
10/16/2012

Input: EUCLID(X,Y)

EXPORT EUCLID(X,Y)
BEGIN
LOCAL M,N,R,A;
MAX(X,Y)→ N;
MIN(X,Y)→M;
PRINT();
REPEAT
INT(N/M)→ A;
N - A*M→ R;
PRINT(A + "*" + M + "+" + R);
WAIT(1);
M→ N;
R→ M;
UNTIL R==0;
RETURN N;
END;




This blog is property of Edward Shore. 2012





TI-84 Plus CE: Logistic Map

TI-84 Plus CE: Logistic Map It feels like forever since I with the TI-84 Plus CE. Introduction The program LOGISTIC plots ...