Showing posts with label GCD. Show all posts
Showing posts with label GCD. Show all posts

Sunday, May 26, 2024

TI 30Xa Algorithms: Greatest Common Divisor

TI 30Xa Algorithms: Greatest Common Divisor


To find the greatest common divisor between two positive integers U and V:


Let U ≥ V. Let U = A * V + R where A is the quotient of U / V and R is the remainder. If R≠0, then V becomes the new U and R become the new V. The process repeats until R=0. At that point the value of V prior to the last calculation is the greatest common divisor (GCD) of U and V.


Example:

gcd(166, 78)

U = 166, V = 78


Algorithm Loop:

  1. A = int(U / V)

  2. R = U – V * int(U / V)



A

R

U

V

Start

n/a

n/a

166

78

A = int(166 / 78) = 2

R = 166 – 2 * 78 = 10

2

10

78

10

A = int(78 / 10) = 7,

R = 78 – 7 * 10 = 8

7

8

10

8

A = int(10 / 8) = 1

R = 10 – 1 * 8 = 2

1

2

8

2

A = int(8 / 2) = 4

R = 8 – 4 * 2 = 0

4

0 *STOP*





Procedure


  1. Store the greater of the two numbers in memory register 1: [ STO ] [ 1 ].

  2. Store the lesser of the two numbers in memory register 2: [ STO ] [ 2 ].

  3. Divide memory register 1 by memory register 2. Store the integer part (no fractional part) in memory register 3: [ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ], (integer part) [ STO ] [ 3 ]

  4. Figure the remainder and store the result in memory 3: [ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ] [ STO ] [ 3 ]

  5. If the remainder is 0, stop. The GCD is stored in memory 2.

  6. If the remainder is non-zero, then store memory 2 into memory 1 then memory 3 into memory 2. You need to do it in this order. [ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]. Go back to Step 3 and repeat.


Examples


Example 1: GCD(26, 14)

M1 = 26, M2 = 14



M1

M2

M3

26 [ STO ] [ 1 ], 14 [ STO ] [ 2 ]

26

14


[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.857142857

1 [ STO ] [ 3 ]

26

14

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 12

[ STO ] [ 3 ]

R is not zero, so we continue.

26

14

12

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

14

12

12

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.166666667

1 [ STO ] [ 3 ]

14

12

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 2

[ STO ] [ 3 ]

R is not zero, so we continue.

14

12

2

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

12

2

2

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 6

6 [ STO ] [ 3 ]

12

2

6

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 0

[ STO ] [ 3 ]

R is zero, so we stop.

GCD: [ RCL ] [ 2 ]: GCD(26, 14) = 2

12

2

0



Example 2: GCD(27, 15)

M1 = 27, M2 = 15




M1

M2

M3

27 [ STO ] [ 1 ], 15 [ STO ] [ 2 ]

27

15


[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.8

1 [ STO ] [ 3 ]

27

15

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 12

[ STO ] [ 3 ]

R is not zero, so we continue.

27

15

12

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

15

12

12

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.25

1 [ STO ] [ 3 ]

15

12

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 3

[ STO ] [ 3 ]

R is not zero, so we continue.

15

12

3

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

12

3

3

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 4

4 [ STO ] [ 3 ]

12

3

4

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 0

[ STO ] [ 3 ]

R is zero, so we stop.

GCD: [ RCL ] [ 2 ]: GCD(27, 15) = 3

12

3

0



I hope you find this useful. What I hope to do with the monthly series is to demonstrate various calculations with the TI-30Xa.


Note: For June and July 2024, I will be posting on Saturdays only. I plan to resume the Saturday-Sunday schedule in August.



Eddie


All original content copyright, © 2011-2024. 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.

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. 


Thursday, July 14, 2016

HP 12C Programming Part I: Modulus, GCD, PITI

HP 12C Programming Part I:  Modulus, GCD, PITI 

The last stop (for now) on my 1980s tour is the Hewlett Packard HP 12C calculator, one of the most popular financial calculators of all time.  The HP 12C was first manufactured in 1982 and has been seen ever since.

Today, there are two versions of the HP 12C:  the original and the Platinum.  The original is RPN only and has 99 programming steps of memory.  The Platinum edition, first released in 2003, has room for 400 steps and includes Algebraic mode.

In this series, I'll concentrate on the 1982 classic.  Albeit, using one manufactured this decade, the processing speed in today's HP 12C's compared to those produced in the 1980s is tremendous.


HP 12C Modulus

This program takes the modulus of two numbers:
Y MOD X = X * FRAC(Y/X)
In this program, X > 0 and Y > 0.

STEP
CODE
KEY
01
10
÷
02
43, 36
LST x
03
34
X<>Y
04
43, 24
FRAC
05
20
*
06
43, 33, 00
GTO 00

Input:  Y [ENTER] X [R/S],  
Result:  Y MOD X

Test 1:  124 MOD 77  = 47
Test 2: 3862 MOD 108 = 82

HP 12C Greatest Common Divisor (GCD)

This program calculates the greatest common divisor of two integers.  You can enter the two integers in either order.

Program:
STEP
CODE
KEY
COMMENT
01
43, 34
X≤Y
Determine which integer is greater
02
34
X<>Y

03
44, 1
STO 1
R1 = max(X,Y)
04
34
X<>Y

05
44, 0
STO 0
R0 = min(X,Y)
06
45, 1
RCL 1
Begin Euclidian division routine
07
45, 1
RCL 1
Recall R1 twice
08
45, 0
RCL 0

09
10
÷

10
43, 25
INTG

11
45, 0
RCL 0

12
20
*

13
30
-

14
43, 35
X=0

15
43, 33, 21
GTO 21

16
34
X<>Y

17
44, 1
STO 1

18
34
X<>Y

19
44, 0
STO 0

20
43, 33, 06
GTO 06

21
45, 0
RCL 0

22
43, 33, 00
GTO 00



Input:  integer [ENTER] integer [R/S]
Result: GCD

Test 1:   GCD(142,25)
Input:  142 [ENTER] 25 [R/S]
Result: 1

Test 2:  GCD(2555, 1365).   Result: 35

HP 12 PITI (Principal, Interest, Taxes, Insurance)

Here is a simple program that calculates the payment (principal and interest) while taking property taxes and insurance in consideration.  Property taxes and insurance are assumed to be combined as an annual amount.  Payments are assumed to be on a monthly basis.

Program:
STEP
CODE
KEY
COMMENT
01
1
1

02
2
2

03
10
÷
(tax + ins)/12
04
44, 0
STO 0

05
14
PMT

06
14
PMT
Need to press PMT twice.  Second press calculates payment.
07
45, 0
RCL 0

08
16
CHS

09
40
+
PITI is assumed to be an outflow
10
43, 33, 00
GTO 00
Ends the program

Input:
Number of Years [ g ] [ n ] (12*)
Annual Interest Rate [ g ] [ i ] (12÷)
Loan Amount [PV]
Annual Property Insurance and Taxes [R/S]
Result:  PITI

Test:  Calculate PITI for a 30-year loan of $205,000.  The rate on the loan is 4.1%.  Annual insurance and property taxes are estimated to be $1,102.50.
Input:  30 [ g ] [ n ] (12*), 4.1 [ g ] [ i ] (12÷), 205000 [PV], 1102.50 [R/S]
Result:  -1,082.43  (PITI = $1,082.43)

This is blog is proerpty of Edward Shore, 2016.




Numworks (Python): Parallelograms Described by Vectors

Numworks (Python): Parallelograms Described by Vectors Introduction The script drawpgram.py draws a parallelogram constructed by ...