Showing posts with label HP 71B. Show all posts
Showing posts with label HP 71B. Show all posts

Saturday, August 3, 2024

HP 71B: Basic Expanded RPN Program (RPNT)

 HP 71B: Basic Expanded RPN Program (RPNT)


Introduction


This program is an expansion of RPNBASIC that was posted on May 12, 2024. See the original program here (which is itself re-posted from 2016):


https://edspi31415.blogspot.com/2016/08/hp-71b-basic-rpn.html


Changes:

* All registers are now represented with single letter variables.


* Added functions: trigonometric and inverse trigonometric functions, random numbers, factorial of positive integers, degree/radian conversion, polar/rectangular conversion, memory-plus function (M+)


* Code fixed to take the lesser memory bytes possible.


Memory Used:


U = temporary register

X = x stack

Y = y stack

Z = z stack

T = t stack

L = last x register

M = memory register



Keys


Stack Operation


[ END LINE ]: Enters a number. The number is placed on the X stack while everything else is pushed up one level. Note: The key string code for END LINE is #38.


[ ↑ ]: Swap: swaps the contents of X and Y stacks. The key string code for ↑ is #50.


[ ↓ ]: Roll Down: brings the stack contents down one level (T to Z, Z to Y, Y to X, X wraps around to T). The key string code for ↓ is #51.


[ f ] [ ↓ ] (-LINE): Drop: Drops the stack down, Y replaces X, Z goes to Y, and T is copied to Z. This is effectively erasing the contents of stack X, and is similar to the HP 48’s DROP command. The key string code for -LINE is #107.


[ f] [ ) ] (COPY): Duplicate: Duplicates the contents of the X stack to the Y stack, after Y transfer to Z and Z transfers to T. This command is similar to the HP 48’s DUP command. The key string code for COPY is f) (small F, right parenthesis).


[ P ]: Pi (π): Enters the numeric constant π to the X stack and pushes everything else up. The contents of the T stack are lost.


[ M ]: Stores the contents of X into the memory register.


[ S ]: Adds the contents of X to the memory register (M+).


[ g ] [ M ] (m): Recalls the contents of the memory register to the X stack and pushes everything else up. The contents of the T stack are lost.


[ f ] [ END LINE ] (RES): Recalls the contents of the last X register to the X stack and pushes everything else up. The contents of the T stack are lost. The key string code for RES is #94.


[ N ]: Negate: changes the sign of the X stack. This is the change sign key.



Arithmetic and Two-Argument Operations


All these operations update the Last X register. The contents of the Z register get copied to the Y register, then the contents of T gets copied to Z.


[ + ]: Addition: Y + X.


[ - ]: Subtraction. Y – X.


[ * ]: Multiplication. Y * X


[ / ]: Division: Y / X


[ g ] [ / ] (^) : Power: Y^X


[ g ] [ 5 ] (%): Modulus. mod(Y,X)



One-Argument Operations


All these operations update the Last X register. No other stack levels are affected.


[ f ] [ / ] (SQR): Principal (positive) Square Root of X. The key string code for SQR is f/ (small f, division).


[ U ]: Square of X. (X^2 = X * X).


[ I ]: Reciprocal of X. (1/X)


[ F ]: Fractional part of X.


[ g ] [ F ] (f): Integer part of X.


[ f ] [ - ] (LOG): Natural logarithm of X. ln(X) The key string code for LOG is f- (small f, subtraction).


[ f ] [ * ] (EXP): Exponential of X. e^X (e is about 2.71828...) The key string code for EXP is f* (small f, multiplication).


[ f ] [ 4 ] (SIN): Sine of X.


[ f ] [ 5 ] (COS): Cosine of X


[ f ] [ 6 ] (TAN): Tangent of X


[ f ] [ 1 ] (ASIN): Arc-sine of X


[ f ] [ 2 ] (ACOS): Arc-cosine of X


[ f ] [ 3 ] (ATAN): Arc-tangent of X


[ D ]: Change to Degrees mode


[ g ] [ D ] (d): Convert X from radians to degrees


[ R ]: Change to Radians mode


[ g ] [ R ] (r): Convert X from degrees to radians


[ g ] [ 3 ] (#): Generate a random number between 0 and 1.


[ f ] [ = ] (FACT): Factorial of X. X must be a positive integer.


[ f ] [ , ] ( > ): Convert rectangular to polar. X: x-coordinate to radius, Y: y-coordinate to angle


[ f ] [ . ] ( < ): Convert polar to rectangular. X: radius to x-coordinate, Y: angle to y-coordinate



HP 71B Basic Program Code: RPNT

Memory: 1253 bytes 


10 DESTROY U,X,Y,Z,T,M,L,K$

15 DIM U,X,Y,Z,T,M,L,K$

20 DISP “RPN BASIC” @ WAIT 1

22 DISP “EXIT = E” @ WAIT 1

25 DELAY 0,0

30 DISP “X: “; X

35 K$ = KEY$

40 IF K$=”E” THEN DISP “THANK YOU.” @ STOP


60 IF K$=”#38” THEN 300

62 IF K$=”#50” THEN U=X @ X=Y @ Y=U

64 IF K$=”#51” THEN U=X @ X=Y @ Y=Z @ Z=T @ T=U

66 IF K$=”#107” THEN X=Y @ Y=Z @ Z=T

68 IF K$=”f)” THEN T=Z @ Z=Y @ Y=X

70 IF K$=”P” THEN U=PI @ GOTO 304

72 IF K$=”M” THEN M=X

73 IF K$=”S” THEN M=X+M

74 IF K$=”m” THEN U=M @ GOTO 304

76 IF K$=”#94” THEN U=L @ GOTO 304

78 IF K$=”N” THEN X=-X


80 IF K$=”+” THEN U=Y+X @ GOTO 310

82 IF K$=”-” THEN U=Y-X @ GOTO 310

84 IF K$=”*” THEN U=Y*X @ GOTO 310

86 IF K$=”/” THEN U=Y/X @ GOTO 310

88 IF K$=”^” THEN U=Y^X @ GOTO 310

90 IF K$=”%” THEN U=MOD(Y,X) @ GOTO 310


100 IF K$=”f/” THEN L=X @ X=SQR(X)

102 IF K$=”U” THEN L=X @ X=X^2

104 IF K$=”A” THEN L=X @ X=ABS(X)

106 IF K$=”I” THEN L=X @ X=1/X

108 IF K$=”F” THEN L=X @ X=FP(X)

110 IF K$=”f” THEN L=X @ X=IP(X)

112 IF K$=”f-” THEN L=X @ X=LOG(X)

114 IF K$=”f*” THEN L=X @ X=EXP(X)

116 IF K$=”f4” THEN L=X @ X=SIN(X)

118 IF K$=”f5” THEN L=X @ X=COS(X)

120 IF K$=”f6” THEN L=X @ X=TAN(X)

122 IF K$=”f1” THEN L=X @ X=ASIN(X)

124 IF K$=”f2” THEN L=X @ X=ACOS(X)

126 IF K$=”f3” THEN L=X @ X=ATAN(X)

128 IF K$=”D” THEN DEGREES

130 IF K$=”R” THEN RADIANS

132 IF K$=”d” THEN L=X @ X=DEG(X)

134 IF K$=”r” THEN L=X @ X=RAD(X)

136 IF K$=”#” THEN L=X @ U=RND @ GOTO 304

138 IF K$=”f=” THEN L=X @ X=FACT(X)

140 IF K$=”>” THEN L=X @ X=SQR(L^2+Y^2) @ Y=ANGLE(L,Y)

142 IF K$=”<” THEN L=X @ X=L*COS(Y) @ Y=L*SIN(Y)

199 GOTO 30


300 INPUT “NUMBER? “; U

302 ! STACK

304 T=Z @ Z=Y @ Y=X @ X=U

306 GOTO 30


310 ! 2 ARG

312 L=X @ X=U @ Y=Z @ Z=T

314 GOTO 30



Example Calculations


1. Find the percent change from 8000 to 12000. Formula: (12000 – 8000) / 8000 * 100


Keystrokes:

[END LINE] 12000 [END LINE]

[END LINE] 8000 [END LINE]

[ - ]

[ f ] [END LINE] (RES) <LASTx>

[ / ]

[END LINE] 100 [END LINE]

[ * ]


Result: 50 (50%)



2. e^(2*π – 1)


Keystrokes:

[END LINE] 2 [END LINE]

[ P ] <enter π to the stack>

[ * ]

[END LINE] 1 [END LINE]

[ - ]

[ f ] [ * ] (EXP) <exp(x) =e^x>


Result: 196.996371


3. 1/(1/9 + ln 11 + 8^2)


Keystrokes:

[END LINE] 9 [END LINE]

[ I ] <1/x>

[END LINE] 11 [END LINE]

[ f ] [ - ] (LOG) <ln(x)>

[ + ]

[END LINE] 8 [END LINE]

[ U ] <x^2>

[ + ]

[ I ]


Result: 1.50355576541E-2


4. Use the memory register to calculate: 3 * 18 + 8 * 17


Keystrokes:

[END LINE] 3 [END LINE]

[END LINE] 18 [END LINE]

[ * ]

[ M ] <store into memory>

[END LINE] 8 [END LINE]

[END LINE] 17 [END LINE]

[ * ]

[ S ] <add to memory>

[ g ] [ M ] (m) <recall memory>


Result: 190


5. Find the arc-sin of 0.5 in degrees and radians


Keystrokes:

[ D ] <set degrees mode>

[END LINE] 0.5 [END LINE]

[ f ] [ ) ] (COPY) < copies 0.5 to Y >

[ f ] [ 1 ] (ASIN) < Result: 30 degrees >

[ ↑ ] < swap >

[ R ] < set radians mode, RAD indicator is on >

[ f ] [ 1 ] (ASIN) < Result: 0.523598775598 radians >



6. Calculate | 99 – 164 * 1.03^3 |


Keystrokes:

[END LINE] 99 [END LINE]

[END LINE] 164 [END LINE]

[END LINE] 1.03 [END LINE]

[END LINE] 3 [END LINE]

[ g ] [ / ] (^)

[ * ]

[ - ]

[ A ] < absolute value >


Result: 80.207228


7. Convert Rectangular to Polar with X = 4, Y = 5. Calculate the angle in degrees mode.


Keystrokes:

[ D ] <set degrees mode>

[END LINE] 5 [END LINE] <enter y first>

[END LINE] 4 [END LINE] <enter x next>

[ g ] [ , ] ( > ) < rectangular to polar>


Display: 6.40312423743 (radius)

[ ↑ ] <swap>

Display: 51.3401917459 (angle)


8. Convert Polar to Rectangular with R = 11, θ = 60°.


Keystrokes:

[ D ] <set degrees mode>

[END LINE] 60 [END LINE] <enter θ first>

[END LINE] 11 [END LINE] <enter r next>

[ g ] [ . ] ( < ) <polar to rectangular>


Display: 5.5 (x)

[ ↑ ]

Display: 9.52627944162



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, May 12, 2024

HP 71B: Basic RPN Program

HP 71B: Basic RPN Program



Repost


This is a repost of a basic program for the HP 71B. I received an email from Kenneth who pointed out that line 145 lead to an error. In further testing, this lead to the input routine not working correctly. The following code in today’s repost is a corrected algorithm. Edited lines are in blue (lines 5, 125, and 145).


The original entry, posted on August 29, 2016, will be deactivated.


HP 71B Program: BASIC


The program RPNBASIC puts the HP 71B into RPN mode with arithmetic functions, power, square root, and π.  The program contains room for one independent memory and a four level stack that works like the classic Hewlett Packard RPN calculators. 


I have the array S set up for six slots:

Slot 1: X stack (display)

Slot 2: Y stack

Slot 3: Z stack

Slot 4: T stack

Slot 5: Independent memory stack

Slot 6: (temporary memory)


For an idea of how RPN works, check out this tutorial:  http://edspi31415.blogspot.com/2011/09/rpn-basics.html


As a note, this program requires you to press the equals key [ = ] before you enter a number to the stack.  


Example:   2 + 3 + 9 = 14

Keys:

[ = ], 2, [END LINE]

[ = ], 3, [END LINE], [ + ], 

[ = ], 9,  [END LINE], [ + ]



The keys available during RPNBASIC:

[ = ] Input 

[ + ] Add: Y + X

[ - ] Subtract: Y - X

[ * ] Multiplication: Y * X 

[ / ] Divide: Y/X

[ g ] [ / ] (^) Exponent: Y^X

[ Q ]  Square Root √X

[ P ] Enter π to the stack 

[ M ] Store X in independent memory

[ R ] Recall memory 

[ C ] Clear X stack to 0


Stack operations:

[ S ] Swap with X and Y

[ D ] Roll stack down, result { Y, T, Z, X }


Exit the program: press [ E ]



HP 71B Program: RPNBASIC  (828 bytes)

5 ! SIMPLE RPN, EWS 5/12/2024

10 DESTROY S, K$

15 DIM K$, S(6)

20 OPTION BASE 1

25 DISP "RPN BASIC" @ WAIT 1

30 DELAY 0,0

35 DISP S(1)

40 K$=KEY$

50 IF K$="+" THEN S(6)=S(1)+S(2) @ GOTO 100

52 IF K$="-" THEN S(6)=S(2)-S(1) @ GOTO 100

54 IF K$="*" THEN S(6)=S(1)*S(2) @ GOTO 100

56 IF K$="/" THEN S(6)=S(2)/S(1) @ GOTO 100

58 IF K$="=" THEN 140

60 IF K$="M" THEN S(5)=S(1) @ GOTO 35

62 IF K$="R" THEN S(6)=S(5) @ GOTO 120

64 IF K$="P" THEN S(6)=PI @ GOTO 120

66 IF K$="C" THEN S(1)=0 @ GOTO 35

68 IF K$="S" THEN 160

70 IF K$="D" THEN 180

72 IF K$="Q" THEN S(1)=SQR(S(1)) @ GOTO 35

74 IF K$="^" THEN S(6)=S(2)^S(1) @ GOTO 100

76 IF K$="E" THEN 200 ELSE 40

100 ! 2 STACK OPERATION

105 S(1)=S(6) @ S(2)=S(3) @ S(3)=S(4)

110 GOTO 35

120 ! PI/RCL/INPUT

125 S(4)=S(3) @ S(3)=S(2) @ S(2)=S(1) @ S(1)=S(6)

130 GOTO 35

140 ! INPUT NUMBER

145 INPUT "NUMBER:";S(6)

150 GOTO 120

160 ! SWAP 

165 S(6)=S(1) @ S(1)=S(2) @ S(2)=S(6)

170 GOTO 35

180 ! ROLL

185 S(6)=S(1)

190 S(1)=S(2) @ S(2)=S(3) @ S(3)=S(4) @ S(4)=S(6)

195 GOTO 35

200 STOP


Examples:


Remember the order of operations! 


Example 1:

22.5 - 12.3 * 3.3 = -18.09

[ = ], 12.3, [ END LINE ],

[ = ], 3.3, [ END LINE ], [ * ],

[ = ] 22.5, [ END LINE ], [ S ], [ - ]


Example 2: (5^2 + 1.4^3)/2 = 13.872

[ = ], 5, [ END LINE ], [ = ], 2, [ END LINE ], [ g ], [ / ] (^)

[ = ], 1.4, [ END LINE ], [ = ], 3, [ END LINE ], [ g ], [ / ] (^), [ + ]

[ = ], 2, [ END LINE ], [ / ]


Example 3: π * 3.12^2 + (3.12 * 1.99) = 36.7903195271

Store 3.12 in memory. 

[ = ], 3.12, [ END LINE ], [ M ],

[ = ], 2 , [ END LINE ], [ g ], [ / ] (^),

[ P ], [ * ],

[ R ], [ = ], 1.99, [ END LINE ], [ * ], [ + ] 


Enjoy!


Note: this program can be expanded to include additional functions and calculations.  



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, December 17, 2023

HP 15C and HP 71B: Precession of the Equinoxes

HP 15C and HP 71B:   Precession of the Equinoxes



How Far Does the Celestial Object Move?  


Given a celestial object's right ascension (α) and declination (δ), we can calculate the new positions after N years by the formulas presented by The Cambridge Handbook of Physics Formulas (see Source):


α' ≈ α0 + (3.075" + 1.336" × sin α0 × tan δ0) × N


δ' ≈ δ0 + (20.043" × cos α0) × N



Decimal degrees of seconds:


3.075" ≈ 854.1677 × 10^-6


1.336" ≈ 371.1111 × 10^-6


20.043" ≈ 5.5675 × 10^-3



According to the Handbook, the formulas are good for only several centuries, as the formulas are local approximations.  




HP 15C Code:  New Right Ascension (LBL A) and New Declination (LBL D)


Step:  Key Code:  Key



New Right Ascension, α'


001:  42, 21, 11:  LBL A

002:  __, 43, _7:  DEG

003:  __, __, _1:  1

004:  __, __, _3:  3

005:  __, __, _3:  3

006:  __, __, 48:  .

007:  __, __, _6:  6

008:  __, __, 26:  EEX

009:  __, __, _6:  6

010:  __, __, 16:  CHS

011:  __, 43, _2:  →H

012:  __, 45, _1:  RCL 1

013:  __, 43, _2:  →H

014:  __, __, 23:  SIN

015:  __, __, 20:  ×

016:  __, 45, _2:  RCL 2

017:  __, 43, _2:  →H

018:  __, __, 25:  TAN

019:  __, __, 20:  × 

020:  __, __, _3:  3

021:  __, __, _0:  0

022:  __, __, _7:  7

023:  __, __, 48:  .

024:  __, __, _5:  5

025:  __, __, 26:  EEX

026:  __, __, _6:  6

027:  __, __, 16:  CHS

028:  __, 43, _2:  →H

029:  __, __, 40:  +

030:  45, 20, _3:  RCL× 3

031:  __, 45, _1:  RCL 1

032:  __, 43, _2:  →H

033:  __, __, 40:  +

034:  __, 42, _2:  →H.MS

035:  __, 44, _4:  STO 4

036:  __, 43, 32:  RTN


New Declination:  δ'


037:  42, 21, 14:  LBL B

038:  __, 43, _7:  DEG

039:  __, __, _2:  2

040:  __, __, 48:  .

041:  __, __, _0:  0

042:  __, __, _0:  0

043:  __, __, _4:  4

044:  __, __, _3:  3

045:  __, __, 26:  EEX

046:  __, __, _3:  3

047:  __, __, 16:  CHS

048:  __, 43, _2:  →H

049:  __, 45, _1:  RCL 1

050:  __, 43, _2:  →H

051:  __, __, 24:  COS

052:  __, __, 20:  × 

053:  45, 20, _3:  RCL× 3

054:  __, 45, _2:  RCL 2

055:  __, 43, _2:  →H

056:  __, __, 40:  +

057:  __, 42, _2:  →H.MS

058:  __, 44, _5:  STO 5

059:  __, 43, 32:  RTN



Variables Used:


R1 = α0:  Initial Right Ascension (enter in DD.MMSS format)

R2 = δ0:  Initial Declination  (enter in DD.MMSS format)

R3 = N:  Number of Years from 2000.  


Outputs:


R4 = α':  Final Right Ascension (in DD.MMSS format)

R5 = δ':  Final Declination  (in DD.MMSS format)



HP 71B Code:   PRECES


Note:  I had battery problems with the HP 71B, so I'm writing this code from written notes.  


100  DEGREES

110  PRINT "PRECESSION" @ WAIT 0.25

120  PRINT "EPOCH J2000.0"  @ WAIT 0.25

130  INPUT "R.A. °,M,S ?"; H,M,S

140  GOSUB 500 @ A0 = X

150  INPUT "DEC °,M,S? "; H,M,S

160  GOSUB 500 @ D0 = X

170  INPUT "# YEARS? "; N

180  A1 = A0 + (854.1667E-6 + 371.1111E-6 * SIN(A0) * TAN(D0)) * N

190  D1 = D0 + (5.5675E-3 * COS(A0)) * N

200  X = A1 @ GOSUB 600 

210  H1 = H @ M1 = M @ S1 = S @ G1 = G

220  PRINT "R.A. ADJ=" @ WAIT 0.25

230  PRINT G1*H1; "°"; M1; "m"; S1; "s" @ PAUSE

240  X = D1 @ GOSUB 600

250  H2 = H @ M2 = M @ S2 = S @ G2 = G

260  PRINT "DEC ADJ=" @ WAIT 0.25

270  PRINT G2*H2; "°"; M2; "m"; S2; "s"

280  END


500  X=SGN(H) * (ABS(H) + M/60 + S/3600)

510  RETURN


600  G = SGN(X) @ X=ABS(X) @ H=IP(X)

610  M = IP(FP(X) * 60)

620  S = FP(FP(X) * 60) * 60

630  RETURN



Examples



First Point of Aries (Vernal Equinox)


α0 = 0° 00' 00"

δ0 = 0° 00' 00"


N = 100

α' ≈ 5'08" (0.0508)

δ' ≈ 33'24"  (0.3324)



Regulus (Alpha Leonis (Leo))


α0 ≈ 5° 55' 10"

δ0 ≈ 7° 24' 25"


N = 100

α' ≈ 6° 00' 19"

δ' ≈ 7° 57' 39"  


Sagittarius A*  (Center of the Milky Way Galaxy)


α0 ≈ 17° 45' 40"

δ0 ≈ -29° 00' 28"


N = 100

α' ≈ 17° 50' 25"

δ' ≈ -28° 28' 39"  



Betelgeuse (Alpha Orionis (Orion))


α0 ≈ 10° 28' 22"

δ0 ≈ 11° 58' 02"


N = 100

α' ≈ 10° 13' 34"

δ' ≈ 12° 30' 55"  



Source


Woan, Gaham.   The Cambridge Handbook of Physics Formulas  2003 Edition. Cambridge University Press.  2000. ISBN 978-0-511-07589-6



Eddie


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

HP 71B and HP 75C: Degrees and DMS Conversions

HP 71B and HP 75C:  Degrees and DMS Conversions



At default, the HP 71B and HP 75C do not have conversions between degrees and degrees-minutes-seconds.   Here are two short programs to remedy this and can be used as subroutines.  The language used is BASIC.



HP 71B and HP 75C Code:  DMS to Degrees.  File Name: DMS2D


10 INPUT "d, m, s? ": H, M, S

20 D=SGN(H)*(ABS(H)+M/60+S/3600)

30 PRINT D; "°"




HP 71B and HP 75C Code:   Degrees to DMS.   File Name:  D2DMS


10  INPUT "DEC DEG? "; D

20 G=SGN(D) @ D=ABS(D) @ H=IP(D)

30 M=IP(FP(D)*60)

40 S=FP(FP(D)*60)*60

50 PRINT G*H; "°"; M; "m"; S; "s"



Notes:  


1.  The degree character, °, can be typed by the following combinations:

HP 71B:  g, CTRL, A

HP 75C:  CTL + A


2.  When entering DMS, separate degrees, minutes, and seconds by commas.  For negative degrees in DMS format, enter the negative sign in front of degrees.  


3.  Repeating decimals (like 42.606666666...) may produce approximate conversions.  



Examples to Try


43.25° ←→ 43° 15m 0s 


-63.27°  ←→ -63° 16m 12s


42.60166667° ←→ 42° 36m 6s  (about)




Eddie


All original content copyright, © 2011-2023.  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, October 30, 2022

HP 71B and TI-84 Plus CE: Binary Decimal Conversions Revised

HP 71B and TI-84 Plus CE:  Binary Decimal Conversions Revised


The programs presented can easily convert integers without the needs of lists or strings in input.  



HP 71B Programs:  BINDEC, DECBIN


No modules are required.


BINDEC (137 bytes)


100 DISP "BIN>DEC" @ WAIT .5

105 DESTROY A,B,C,D,E

110 INPUT "BIN? ";A

115 B=1 @ D=0 @ C=A

200 E=A/10 @ A=INT(E)

205 D=D+10*B*(E-A) @ B=2*B

210 IF A THEN 200

300 DISP "(2)";C;"="D


DECBIN (130 bytes)


100 DISP "DEC>BIN" @ WAIT .5

105 DESTROY A,B,C,D

110 INPUT "DEC? ";D

115 B=0 @ A=D @ C=1

200 E=D/2 @ D=INT(E)

205 B=B+C*(D<E)

210 C=C*10

215 IF D THEN 200

300 PRINT A;"=(2)";B



TI-84 Plus CE Programs:  BINDEC2, DECBIN2



BINDEC2


"V.2"

Disp "BIN>DEC"

Input "BIN? ",A

1→B:0→D:A→C

Lbl 2

A/10→E:iPart(E)→A

D+10*B*(E-A)→D

2*B→B

If A:Goto 2

Disp "(2) "+toString(C)+"= "+toString(D)



DECBIN2


"V.2"

Disp "DEC>BIN"

Input "DEC? ",D

0→B:D→A:1→C

Lbl 2

D/2→E:iPart(E)→D

B+C*(D<E)→B

10*C→C

If D:Goto 2

Disp toString(A)+"=(2) "+toString(B)


This is recommended for only integer values of 512 (decimal base) or less.  


Example


Decimal: 476  (DEC>BIN)

Result Binary:  111011100


Binary:  10111011 (BIN>DEC)

Result Decimal:  187



Source


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



Happy computing,

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. 


Sunday, October 23, 2022

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. 


Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...