Tampilkan postingan dengan label Programming. Tampilkan semua postingan
Tampilkan postingan dengan label Programming. Tampilkan semua postingan

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. 


HP 15C: Weibull Distribution Calculations

HP 15C:  Weibull Distribution Calculations


Introduction


The Weibull probability density distribution function is:


f(x) = (b / Θ) * (x / Θ)^(b-1) * exp(-(x / Θ)^b)


with the lower tail cumulative distribution of (-∞ to x):


Area = 1 - exp(-(x / Θ)^b)


The area function tells us what is the probability a device lasts no more than x time units.  


Area = 1 - Survival


The survival function is the probability a device lasts more than x time units.


Survival = exp(-(x / Θ)^b)


Generally, the higher Θ is, the flatter the Weibull Distribution curve.  


What follows are four calculations regarding the Weibull Distribution.  In the following programs, store the following values first prior to running the programs:


R0 = x

R1 = b

R2 = Θ


Use whatever labels you like.  


HP 15C Program:  Lower Tail Probability - Weibull Distribution


CDF = 1 - exp(-(x/Θ)^b)


Keys:


LBL B

1

RCL 0

RCL÷ 2

RCL 1

y^x

CHS

e^x

-

RTN


Key Codes:


42, 21,12

1

45, 0

45, 10, 2

45, 1

14

16

12

30

43, 32


Example:  

b = 1.96, Θ = 420

x = 300, result:  0.4038

x = 400, result:  0.5970

x = 500, result:  0.7552


HP 15C Program:  Failure Rate - Weibull Distribution


FR = b/Θ * (x/Θ)^(b-1) 


Keys:


LBL C

RCL 1

RCL÷ 2

RCL 0

RCL÷ 2

RCL 1

1

-

y^x

*

RTN


Key Codes:


42, 21, 13

45, 0

45, 10, 2

45, 0

45, 10, 2

45, 1

1

30

14

20

43, 32


Example:  

b = 1.96, Θ = 420

x = 300, result:  0.0034

x = 400, result:  0.0045

x = 500, result:  0.0055


HP 15C Program:  Mean of a Weibull Distribution


µ = (1/b)! * Θ


Keys:


LBL D

RCL 1

1/x

x!

RCL× 2

RTN


Key Codes:


42, 21, 14

45, 1

15

42, 0

45, 20, 2

43, 32


Example:  

b = 1.96, Θ = 420

Result:  373.3720


HP 15C Program:  Standard Deviation of a Weibull Distribution


σ = Θ * √((2/b)! - (1/b)!^2)


Keys:


LBL E

2

RCL 1

÷

x!

RCL 1

1/x

x!

x^2

-

RCL× 2

RTN


Key Codes:


42, 21, 15

2

45, 1

10

42, 0

45, 1

15

42, 0

43, 11

30

11

45, 20, 2

43, 32


Example:

b = 1.96, Θ = 420

Result:  198.2208


Sources:


HP55 Statistics Programs  Hewlett Packard Company.  Cupertino, CA.  1975


Ma, Dan.  "The Weibull distribution"  Topics in Actuarial Modeling.  September 28, 2016.   https://actuarialmodelingtopics.wordpress.com/2016/09/28/the-weibull-distribution/  Last Retrieved September 20, 2022.  



Eddie


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. 


HP Prime: Reversing an Integer's Digits

HP Prime:  Reversing an Integer's Digits


(Inspired by the HHC 2022 programming contest)


What Should I Add To Reverse the Digits?


Let A, B, C, D, and E be individual digits (0-9) of an integer.   AB would represent a two digit integer with the value of 10 * A + B.  ABC would represent a three digit integer with the value of 100 * A + 10 * B + C.


Reversing a Two Digit Integer


AB + # = BA

10 * A + B + # = 10 * B + A

# = 9 * (B - A)


Example:  Let AB = 76.

A = 7, B = 6

# = 9 * (6 - 7) = -9

76 - 9 = 67


Reversing a Three Digit Integer


ABC + # = CBA

100 * A + 10* B + C + # = 100 * C + 10 * B + A

# = 99 * (C - A)


Example:  ABC = 469

# = 99 * (9 - 4) = 495

469 + 495 = 964


Reversing a Four Digit Integer


ABCD + # = DCBA

1000 * A + 100 * B + 10 * C + D + # = 1000 * D + 100 * C + 10 * B + A

# = 999 * (D - A) + 90 * (C - B)


Example:  ABCD = 7219

# = 999 * (9 - 7) + 90 * (1 - 2) = 1908

7219 + 1908 = 9127


Reversing a Five Digit Integer


ABCDE + # = EDBCA

10000 * A + 1000 * B + 100 * C + 10 * D + E + # =

10000 * E + 1000 * D + 100 * C + 10 * B + A 

# = 9999 * (E - A) + 990 * (D - B)


Example: ABCDE = 52693

# = 9999 * (3 - 5) + 990 * (9 - 2) = -13068

52693 - 13068 = 39625


Having the Calculator Do It


The program REVINT reverses the digits of an integer, up to 11 digits.   The program does not allow numbers that have non-zero fractional parts or integers more than 11 digits.  Instead of solving for # (see above), the program splits the integers into a list in reverse order, and uses list processing to get the final answer. 


HP Prime Program:  REVINT


Caution:  Integers that end or begin with zero may not return accurate results.   My suggestion is not use 0s with this program.  See examples below for more details.  


EXPORT REVINT(N)

BEGIN

// 2022-09-18 EWS

// reverse the integer N

// up to 12 digits

LOCAL D,P,A,I,M,L;

L:={};

P:=XPON(N);


// check size 

  IF P>11 THEN

  RETURN "TOO BIG";

  KILL;

  END;

 

// check type

  IF FP(N) THEN

  RETURN "NOT AN INTEGER";

  KILL;

  END;

   

D:=N;


// loop

  FOR I FROM P DOWNTO 0 DO

  A:=D/ALOG(I);

  L:=CONCAT({IP(A)},L);

  D:=D-IP(A)*ALOG(I); 

  END;

  

// rebuild 

M:=ΣLIST(MAKELIST(ALOG(X),X,P,0,−1)*L);

RETURN M; 

END;


Examples:


REVINT(4321) returns 1234


REVINT(56765) returns 56765   (56765 is a palindrome, reversing the digits results in the same number)


REVINT(42910) returns 1924 (01924 - be aware about integers ending or beginning with 0)


REVINT(67.28) returns "NOT AN INTEGER" (error)



Eddie


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. 


hp 9g: Sample Programs

hp 9g:  Sample Programs


Introduction


As I mentioned in yesterday's blog, today's blog will have some sample programs for the hp 9g.   


The hp 9g's programming language is a mix of C and Basic, and is unique from the rest of the Hewlett Packard calculators.    The hp 9g has 400 steps of programming memory to be allocated among 10 program files, P0 through P9.   


The 9g has 26 variables, but can be extended up to 53 variables at the cost of program steps.  The square brackets indicate indirect addressing.  The number in the square brackets is the number of steps away from the variable.   Any variable beyond Z (Z[0], A[25]) must be addressed in this fashion.  


Example: 

A[0] calls up A

A[1] calls up B

...

A[25] calls up Z. 


We can use any letter, but I recommend using either A or Z. 



hp 9g Program:  Integer Division


This program gives the quotient and remainder of N ÷ D.  The program here assumes you enter positive integers, and some code can be entered for checking inputs.  


INPUT N

INPUT D

Q=INT(N/D)

R=N-Q*D

PRINT Q," R",R

END


Examples:


N = 4379, D = 8;  Result:  547 R3


N = 5884, D = 29;  Result:  202 R26



Note:  The PRINT command converts variable values to strings automatically, without the need for a string or STR$ command.


hp 9g Program:  Arithmetic-Geometric Mean


This program determines the AGM given two numbers A and G.


INPUT A

INPUT B

PRINT "ACC. 10^-8"

Lbl 0:

X=.5(A+G)

Y=√(AG)

If (ABS(X-Y)<10^(-8))

Then {Goto 1}

A=X

G=Y

Goto 0

Lbl 1:

Print "ANS= ", X


Examples:


A = 1.3, G = 1.5,   Result:  ANS ≈ 1.39821143


A = 20, G = 45,  Result:  ANS ≈ 31.23749374



Notes:  


10^ is from [ 2nd ] (10^x) the antilog function.


The label command must have the colon character after the label number, or a syntax error occurs.  


If you run a program from the Main mode by the [ PROG ] key, you will be transferred to Program mode when the program finishes.


hp 9G Program: The Maximum Value of A through X



Y = maximum value

Z = counter variable


PRINT "MAX(A:X)"; ◢

FOR(Z=2;Z<23;Z++){

IF(A[Z]>Y)

THEN{Y=A[Z]}};

PRINT "MAX=",Y;

END


Example:  


Clear all variable in the MAIN mode by CL-VAR

Set up variables: 


A = 12, D = 24, E = 37, G = 40, S = 19, T = 16, U = 7

Result:  MAX= 40


Notes:   


++ is from the Instruction menu, it is a smaller-jointed double plus.


When the program encounters a run/stop instruction (◢), the last message is displayed.  Continue execution by pressing the equals key [ = ].  


hp 9g Program:  Plotting y = A * sin(B * x + C) + D


The programming mode does not offer a lot of support when it comes to graphs.  


PRINT "SINE"

SLEEP(.5)

INPUT A,B,C,D

RANGE(-2π,2π,π/4,-A,A,1)

Graph Y=A*sin(BX+C)+D

PRINT "PRESS G<>T"

END




A = 1, B = 1, C = 0, D = 0



Notes:


Prior to running the program, set the angle mode to Radians through the [DRG] key.  Also, press [ 2nd ] [ → ] (CLS) to clear the graph screen.  Unfortunately, neither of these commands can be programmed.  


Plots from programs cannot be traced.


When the program ends, you will need to switch to Main mode, press [G<>T] to show the graph.   Not a very efficient way of showing a graph.  



That is a sample of the programs for the hp 9g.  Good for number crunching, kind of a little bit to be desired for graphics.


Until next time,


Eddie 



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. 


Retro Review: hp 9g

Retro Review:   hp 9g







Quick Facts:


Model:  hp 9g

Company:  Hewlett Packard

Years:  2003 (short production life)

Battery:  2 x AR76 or 2 x LR 44

Display:  10 digits, 2 digit exponent

Logic:  Algebraic - type in expressions the way you write them

Memory:  400 bytes, but can be designated as additional memory registers

Memory Registers:  26, can be extended to 59 memory registers 

Slide Case

Range:  9.999999999 * 10^-99 to 9.999999999 * 10^99, real numbers only


The hp 9g is a small sized graphing calculator, similar to the Citizen SRP-325G and the Casio fx-6300g.  The 9g offers more features than the fx-6300g. 


Features


*  Trig, logs, power, permutations, combinations, hyperbolic functions

*  Random numbers and integers, integer factorials, fraction, integer, sign, absolute value

*  Maximum, minimum, sum, and average up to 10 numbers 

*  Base conversions (decimal, binary, octal, hexadecimal) with Boolean logic (NOT, AND, OR, XOR, NXOR, NEG)

*  20 scientific constants, all SI units   

*  Conversions:  length, area, temperature, volume, mass, calories, pressure (always a good thing to have!)

*  Function graphing, more than one function can be plotted on top of each other.

*  One and Two Variable Statistics

*  Regressions:  linear, logarithmic, exponential, power, inverse, quadratic


Percent


The percent (%) just divides the argument by 100.   For example:  58% converts the number to 0.58.   That is good for multiplying or dividing but not for direct addition and subtraction.


Undo


The [ 2nd ] [ ENTER ] brings back the last thing that has been cleared or deleted.  I don't think this brings back programs that have been deleted though.  


Normal Distribution - One Variable Statistics


Normal distribution is based on one-variable statistics.  The t value is based on data point a_x, which is entered in the DATA menu.  The t point is calculated with the formula:


t = (a_x - mean) / σ


Three areas can be calculated:


P(t):  lower tail cumulative distribution

Q(t):  absolute value of the cumulative distribution between 0 and t

R(t):  upper tail cumulative distribution (I think)


Process Capability - One or Two Variable Statistics


A very unique function, available to the 9G are process capability calculations.  Depending on the mode, the following can be calculated:


C_ax = capability accuracy of x values

C_ay = capability accuracy of y values

C_px = potential precision of x values 

C_py = potential precision of y values

C_pkx = minimum of capability of values or capability precision of x values

C_pky = minimum of capability of values or capability precision of y values

ppm = parts per million (One variable stat mode only)


See the User Manual for formulas.


Storing a Formula


Formulas can be stored into any one of the program areas (P0- P9), using the [ SAVE ] [ PROG ] key sequence.  We can run programs from the Main mode by pressing [ PROG ] with 0 - 9.  Variables are prompted.


Example:  


X^2-3X+1 [ SAVE ] [ PROG ] 9  stores x^2 + 3*x + 1 into program P9.


Evaluating the function at x = -1 and x = 8.5


[ PROG ] 9 [ = ], (formula is displayed), [ = ], [CL/ESC], -1 at the X prompt, [ = ], result:  5


[ PROG ] 9 [ = ], (formula is displayed), [ = ], [CL/ESC], 8.5 at the X prompt, [ = ], result:  47.75


At all prompts, to enter a new number, press [CL/ESC] first, very important.  


Screen


The screen is split up in different sections.  


There are two lines during calculations.  The top line shows what is being calculated, while the bottom line shows the results on the right hand of the screen, in a smaller font.  


In program editing mode, we only get the top line as the bottom of the screen is taken by the two lines EDIT:  and *MAIN* (*DEC/HEX/OCT/BIN* for BASE-N programs).   


In data input, only the top line shows the data points.   


If you graph a function, the left side of the screen is the graph.  Pressing [ Trace ] will alternate between showing the x-coordinate and the y-coordinate.   Personally, I would like it better if both x and y were shown.   The small graph screen means that we really can't do much (no shading, no integration, have to arrow to approximate roots, but can draw lines and plot points).   


Not perfect, but one of the best uses of a small graphics calculator screen.  


Programming


The hp 9g has 400 steps that can be divided among 10 programs (P0 to P9).   Each program can either work in Main mode or Base mode.  Base mode programs are designated for base conversions and Boolean logic.  


The programming language is somewhat like the C programming language.   Each instruction or line can, but doesn't have to end with a semicolon (;).    However, any command called from the INST menu inserts a semicolon.  


Some program commands include:


INPUT var1[, var2, var3... ]  

Input with prompt "[var] = ".  


PRINT "text"/var, "text"/var, ...     

Display strings and variable values


IF (condition) THEN { do if true, commands separated by a semicolon } ; ELSE { do if false, commands separated by a semicolon }

If-Then-Else structure.  The Else part is optional.


Lbl/GOTO n

Lables can take values 0-9


GOSUB PROG n;

Calls a subroutine program n.  Semicolon is required.


SLEEP(time)

Pauses execution for seconds up to 105 seconds.


SWAP(varA, varB)

Swaps the values of variables A and B


; ◢

Stops the program and shows immediate results.   Press [ = ] to continue.


var++, var--   (The ++ and -- is from the INST menu, not pressing + or - twice)

Increase or decrease the variable by 1 after the expression is evaluated.


++var, --var (The ++ and -- is from the INST menu, not pressing + or - twice)

Increase or decrease the varaible by 1 before the epression is evaluated.


FOR(start condition; continue condition; next expression) {loop commands separated by a semicolon}

For loop that works more like a WHILE loop


start condition

WHILE continue condition

loop commands

next expression

END


The FOR loop is in C language.  This is the same syntax of the FOR equation editor command of the Plus42.  


On tomorrow's blog I will have sample programs for the hp 9g.  


Final Thoughts... For Now


I wish the hp 9g had better manuals or manuals that were formatted like a book instead of two large sheets.   But better than nothing and the documentation is detailed.  


The biggest drawbacks are the lack of screen space and small amount of programming steps.  Despite this, the hp 9g is an underrated marvel and is very unique in a line of HP calculators.    


Sources


hp 9g User Manual:  https://literature.hpcalc.org/official/hp9g-ug-en.pdf


hp 9g Examples:  https://literature.hpcalc.org/official/hp9g-htg-en.pdf


"HP 9g"  Wikipedia.   Last edited May 15, 2022.   Last Accessed September 24, 2022.  https://en.wikipedia.org/wiki/HP_9g

 


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. 


HP 71B, TI 84 Plus CE: Egyptian Fractions to Unit Fractions by the Greedy Algorithm

HP 71B, TI 84 Plus CE:  Egyptian Fractions to Unit Fractions by the Greedy Algorithm


Introduction 


An Egyptian fraction consists of a sum of unit fractions.  A unit fraction is a fraction with 1 in its numerator and a positive integer in its denominator.   


Yesterday, we calculated the Egyptian fraction given unit fractions, full simplified.   Today, we will attempt the reverse process, breaking down an Egyptian fraction into a sum of unit fractions.


N/D = 1/A1 + 1/A2 + 1/A3 + 1/A4 + ....


The Algorithm


The Greedy Algorithm is used.  


The Greedy Algorithm attempts to start the expansion by using the division of ceiling(D/N) = int(D/N) + 1.   Please be aware that this may not be the fastest or more efficient way.  


We also have the problem of a lot of calculators inability to display long integers, that is longer than 10 (usually) before switching to scientific notation mode.  In order to accommodate for memory and the calculator's ability to display integers, I set the algorithm to stop should the denominator for the next step exceed 10^8.  In that case, the remainder fraction is displayed.  



TI-84 Plus CE Program EGYPTUNT


"2022-09-04 EWS"

Disp "EGYPT>SUM UNIT FRACS"

Input "NUM? ",N

Input "DEN? ",D

N→M:D→C

Disp "SUM: ":Wait .5

While M≠1

int(C/M)+1→B

Disp "1/"+toString(B)

Pause 

M*B-C→M

B*C→C

If C>1E8:Goto 2

End

Disp "1/"+toString(C)

Stop

Lbl 2

Disp "REMAIN:"

Disp toString(M)+"/"+toString(C)


* This assumes your TI-84 Plus CE is at least has the 5.5 operating system.


HP 71B Program EGYPTUNT


100 DISP "EGYPT>SUM UNIT FRACS" @ WAIT .5

105 DESTROY N,D,C,M

110 INPUT "NUM? ";N

115 INPUT "DEN? ";D

120 M=N @ C=D

125 DISP "SUM=" @ WAIT .5


200 B=INT(C/M)+1

205 DISP "1/"&STR$(B) @ PAUSE

210 M=M*B-C @ C=C*B

215 IF C>10^8 THEN 400

220 IF M#1 THEN 200


300 DISP "1/"&STR$(C)

305 STOP


400 DISP "REMAIN: " @ WAIT .5

405 DISP STR$(M)&"/"&STR$(C)

410 STOP


Examples:


6/7

NUM = 6

DEN = 7

Results:  1/2, 1/3, 1/42


6/7 = 1/2 + 1/3 + 1/42


181/360

NUM = 181

DEN = 360

Results: 1/2, 1/361, 1/129961, REMAIN: 2/33779463120


Note:  Please be aware that this Greedy Algorithm  is only one approach and just because we get a REMAIN message does not conclude that the fraction cannot be broken into a sum of unit fractions.  For example:  181/360 = 1/5 + 1/8 + 1/9 + 1/15


See the source below for more information.


"Greedy algorithm for Egyptian fractions" Wikipedia.  Edited December 10, 2021.  Last Accessed September 4, 2022.  https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions


Eddie


Halloween is just around the corner!


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. 


HP 71B, TI 84 Plus CE: Sum of Egyptian Fractions

HP 71B, TI 84 Plus CE:  Sum of Egyptian Fractions


Introduction 


An Egyptian fraction consists of a sum of unit fractions.  A unit fraction is a fraction with 1 in its numerator and a positive integer in its denominator.   


Today's blog entry will calculate the Egyptian fraction given unit fractions, full simplified.   On tomorrow's entry, we will attempt the reverse process, breaking down an Egyptian fraction into a sum of unit fractions.


N/D = 1/A1 + 1/A2 + 1/A3 + 1/A4 + ....


The Algorithm


Start out with two unit fractions 1/A1 and 1/A2.  


Let N2/D2 = 1/A1 + 1/A2


Then:

N2/D2 = 1/A1 + 1/A2

N2/D2 = (A1 + A2)/(A1*A2)


Let N3/D3 = 1/A3 + N2/D2


Then:  N3/D3 = (A3*N2 + D2)/(N2*D2)


For any general point:


N_x+1/D_x+1 = (N_x * A_x + D_x)/(A_x * D_x)


This allows the user to input as many unit fractions as allowed.  


TI-84 Plus CE Program EGYPTSUM


Disp "EGYPT FRACTION SUM"

"2022-09-03 EWS"

Input "NO. TERMS? ",S

Input "1/A1? ",A

Input "1/A2? ",B

A+B→N

A*B→D

For(I,3,S)

"1/A"+toString(I)+"? "→Str1

Input Str1,A

A*N+D→N

A*D→D

End

gcd(N,D)→G

N/G→N

D/G→D

Disp "NUM= "+toString(N)

Disp "DEC= "+toString(D)

Disp "FRAC="+toString(N/D)


* This assumes your TI-84 Plus CE is at least has the 5.5 operating system.


HP 71B Program EGYPTSUM 


100 DISP "EGYPT FRACTION SUM" @ WAIT .5

105 DESTROY S,A,B,I,N,D,G

110 INPUT "# TERMS? ";S

115 INPUT "1/A1? ";A

120 INPUT "1/A2? ";B

125 N=A+B @ D=A*B

130 IF S=2 THEN 300


200 FOR I=3 TO S:

205 DISP "1/A"&STR$(I)&"?" @ WAIT .5

210 INPUT "1/A? ";A

215 N=A*N+D

220 D=A*D

225 NEXT I


300 A=N @ B=D

305 G=MOD(A,B)

310 A=B @ B=G

315 IF G THEN 305

320 N=N/A @ D=D/A


400 DISP "NUM= ";N

405 DISP "DEN= ";D

410 DISP "FRAC= ";N/D

415 STOP


The 300-325 section calculates the common greatest divisor, based off the "Greatest Common Divisor" program from 119 Practical Programs for the TRS-80 Pocket Computer (see source below).


Examples:


1/2 + 1/6

# TERMS = 2

Result:  2/3 (NUM = 2, DEN = 3)


1/7 + 1/8 + 1/2 

# TERMS = 3

Result:  43/56 (NUM = 43, DEN = 56)


1/9 + 1/5 + 1/15 + 1/8

# TERMS = 4

Result: 181/360 (NUM = 181, DEN = 360)


Source


Craig, John Clark.  119 Practical Programs For The TRS-80 Pocket Computer  TAB Books Inc.  Blue Ridge Summit, PA.  1982 pg. 133



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. 


Backlink 9999 Traffic Super

Order Now...!!!!