A Collection of 20 Elegant Algorithms for the Voyager Series
LBL A ; Entry point - number in X STO 0 ; Store n in R0 2 STO 1 ; Start divisor at 2 LBL 0 ; Loop start RCL 1 xΒ² RCL 0 xβ€y? ; If divisorΒ² > n, it's prime GTO 1 RCL 0 RCL 1 Γ· FRAC x=0? ; If evenly divisible, not prime GTO 2 1 STO+ 1 ; Increment divisor GTO 0 ; Continue loop LBL 1 ; Prime exit 1 RTN LBL 2 ; Composite exit 0 RTN
Enter number, press GSB A. Returns 1 (prime) or 0 (composite).
Examples: 97 β 1 (prime), 91 β 0 (= 7 Γ 13)
LBL B ; Y: a, X: b LBL 0 x=0? ; If b = 0, done GTO 1 LastX ; Recall b x<>y ; Stack: b, a LastX ; Stack: b, a, b Γ· FRAC LastX Γ ; a mod b GTO 0 LBL 1 Rβ ; Result in X RTN
Enter a ENTER b, press GSB B. Returns GCD(a,b).
Example: 48 ENTER 18 GSB B β 6
LBL C ; Initialize: puts 0, 1 in registers 0 STO 0 1 STO 1 RTN LBL 1 ; Generate next Fibonacci RCL 0 RCL 1 STO 0 ; F(n-1) becomes F(n-2) + STO 1 ; F(n) = F(n-1) + F(n-2) RTN
GSB C to initialize, then GSB 1 repeatedly.
Sequence: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
LBL D ; Y=n, X=k, compute C(n,k) STO 0 ; Store k Rβ STO 1 ; Store n RCL 0 RCL 1 RCL 0 - ; n - k x<y? ; Is k > n-k? x<>y ; Use smaller for efficiency STO 0 ; k = min(k, n-k) 1 STO 2 ; Result = 1 1 STO 3 ; i = 1 LBL 0 ; Main loop RCL 3 RCL 0 x<y? ; i > k? GTO 1 ; Done RCL 1 RCL 3 - 1 + ; n - i + 1 STOΓ 2 ; result *= (n-i+1) RCL 3 STOΓ· 2 ; result /= i 1 STO+ 3 ; i++ GTO 0 LBL 1 RCL 2 RTN
Enter n ENTER k, press GSB D.
Examples: C(10,3) = 120, C(52,5) = 2,598,960 (poker hands!)
LBL E ; n in X LBL 0 ; Digit sum loop STO 0 ; Store current n 0 STO 1 ; Digit sum = 0 LBL 1 ; Sum digits loop RCL 0 x=0? GTO 2 ; Done with this round 10 Γ· FRAC 10 Γ ; Extract last digit STO+ 1 ; Add to sum RCL 0 10 Γ· INT ; Remove last digit STO 0 GTO 1 LBL 2 RCL 1 ; Current digit sum 10 x<y? ; Sum < 10? RTN ; Yes: done Rβ RCL 1 ; No: continue reducing GTO 0
Enter number, press GSB E.
Examples: 12345 β 6, 999 β 9, 19683 β 9
LBL .0 ; Enter n in X LBL .1 ; Main loop x=0? RTN ; Safety check 1 x=y? ; Reached 1? RTN ; Done! Rβ 2 Γ· ; n/2 FRAC x=0? ; Was n even? GTO .2 ; Yes: go halve it LastX ; No: recall n/2 2 Γ ; Restore n 3 Γ 1 + ; 3n + 1 PSE ; Pause to display GTO .1 LBL .2 ; Even case LastX ; Recall n/2 2 Γ ; n 2 Γ· ; n/2 (integer) PSE ; Pause to display GTO .1
Enter starting number, press GSB .0.
Watch the sequence unfold with pauses. Ends when reaching 1.
Try: 27 β Takes 111 steps, reaches a peak of 9,232!
LBL .3 ; n = iterations in X STO I ; Save original n STO 0 ; Loop counter 0 STO 1 ; Hits counter = 0 LBL .4 ; Loop RAN# ; Random x β [0,1) xΒ² RAN# ; Random y β [0,1) xΒ² + ; xΒ² + yΒ² 1 xβ€y? ; If xΒ² + yΒ² β€ 1, inside circle 1 STO+ 1 ; Increment hits (conditional) 1 STO- 0 ; Decrement counter RCL 0 xβ 0? GTO .4 ; Continue if counter > 0 RCL 1 ; Recall hits 4 Γ RCL I ; Recall original n Γ· ; 4 Γ hits / n β Ο RTN
Enter iteration count (try 100-500), press GSB .3.
More iterations = better estimate (but slower).
LBL .5 ; n = number of terms in X STO 0 ; Loop counter 1 STO 1 ; Running product 2 STO 2 ; Current even number LBL .6 ; Main loop RCL 2 ; even ENTER ENTER 1 - ; even - 1 Γ· ; even/(even-1) STOΓ 1 ; Multiply into product RCL 2 RCL 2 1 + ; even + 1 Γ· ; even/(even+1) STOΓ 1 ; Multiply into product 2 STO+ 2 ; Next even number 1 STO- 0 ; Decrement counter RCL 0 xβ 0? GTO .6 RCL 1 2 Γ ; Product Γ 2 = Ο approximation RTN
Enter number of terms, press GSB .5.
Results: 10 terms β 3.067..., 100 terms β 3.133..., 1000 terms β 3.140...
LBL .7 ; n in X STO 0 ; Store n 1 RCL 0 Γ· ; 1/n 1 + ; 1 + 1/n RCL 0 y^x ; (1 + 1/n)βΏ RTN
Enter n, press GSB .7.
Convergence: n=10 β 2.5937..., n=100 β 2.7048..., n=10000 β 2.7181...
LBL .8 ; Number in X STO 0 ; Working value LBL .9 ; Extract next coefficient RCL 0 INT ; aβ = floor(x) PSE ; Display coefficient RCL 0 FRAC ; Fractional part x=0? ; If zero, we're done RTN 1/x ; 1 / frac(x) STO 0 ; This is new x GTO .9 ; Continue
Enter number, press GSB .8. Watch coefficients display.
Examples:
Ο β [3; 7, 15, 1, 292, ...] (that 292 explains why 355/113 is so good)
β2 β [1; 2, 2, 2, 2, ...] (all 2s forever!)
Ο β [1; 1, 1, 1, 1, ...] (all 1sβthe "most irrational" number)
LBL 2 ; n in X, compute Hβ STO 0 ; Store n 0 STO 1 ; Sum = 0 1 STO 2 ; k = 1 LBL 3 RCL 2 1/x ; 1/k STO+ 1 ; Add to sum 1 STO+ 2 ; k++ RCL 2 RCL 0 xβ€y? ; k > n? GTO 4 GTO 3 LBL 4 RCL 1 ; Return Hβ RTN
Enter n, press GSB 2.
Values: Hββ = 2.9289..., Hβββ = 5.1873..., Hββββ = 7.4854...
LBL 5 ; n in X, find βn STO 0 ; Store n 2 Γ· STO 1 ; Initial guess = n/2 LBL 6 ; Iteration loop RCL 0 RCL 1 Γ· ; n/x RCL 1 + ; x + n/x 2 Γ· ; New estimate STO 2 ; Store temporarily RCL 1 - ABS EEX 8 CHS ; 1e-8 tolerance x<y? ; Converged? GTO 7 RCL 2 STO 1 ; Update x GTO 6 LBL 7 RCL 2 RTN
Enter number, press GSB 5. Converges rapidly.
Example: 2 GSB 5 β 1.414213562
LBL 8 ; n in X, find βn STO 0 ; Store n 2 Γ· STO 1 ; Initial guess = n/2 LBL 9 ; Iteration loop RCL 0 RCL 1 Γ· ; n/guess RCL 1 + ; guess + n/guess 2 Γ· ; (guess + n/guess)/2 STO 2 ; New guess RCL 1 - ABS 1 EEX 9 CHS ; 10β»βΉ tolerance x>y? ; Converged? GTO .0 RCL 2 STO 1 ; Update guess GTO 9 LBL .0 RCL 2 ; Return βn RTN
Enter number, press GSB 8.
Example: 2 GSB 8 β 1.414213562 (β2)
; First, define your function in LBL .1 ; Example: f(x) = xΒ³ - 2x + 1 LBL .1 ENTER ENTER Γ Γ ; xΒ³ LastX 2 Γ - ; xΒ³ - 2x 1 + ; xΒ³ - 2x + 1 RTN ; Derivative routine LBL .2 ; x in X register STO 0 ; Store x 1 EEX 5 CHS ; h = 0.00001 STO 1 RCL 0 RCL 1 + ; x + h GSB .1 ; f(x + h) RCL 0 RCL 1 - ; x - h GSB .1 ; f(x - h) - ; f(x+h) - f(x-h) RCL 1 2 Γ ; 2h Γ· ; [f(x+h) - f(x-h)] / 2h RTN
Modify LBL .1 with your function. Enter x value, press GSB .2.
Example: For f(x) = xΒ³ - 2x + 1, derivative at x=2:
2 GSB .2 β 10 (exact answer: 3(2)Β² - 2 = 10 β)
LBL .3 ; Enter: Z=a, Y=b, X=c STO 0 ; c in R0 Rβ STO 1 ; b in R1 Rβ STO 2 ; a in R2 RCL 1 xΒ² ; bΒ² RCL 2 RCL 0 Γ 4 Γ ; 4ac - ; discriminant = bΒ² - 4ac SF 8 ; Set complex mode SQRT ; β(discriminant) - complex if negative! STO 3 ; Store βdiscriminant RCL 1 CHS ; -b RCL 3 + ; -b + βd RCL 2 2 Γ Γ· ; First root: (-b + βd)/(2a) RCL 1 CHS RCL 3 - ; -b - βd RCL 2 2 Γ Γ· ; Second root in X, first in Y RTN
Enter a ENTER b ENTER c, press GSB .3.
Press Re<>Im to see imaginary parts.
Example: 1 ENTER 2 ENTER 5 GSB .3
Roots of xΒ² + 2x + 5 = 0 are -1 Β± 2i
; First, configure and enter your matrix ; Example: 2Γ2 matrix f DIM ; Enter matrix dimension mode 2 ENTER 2 f MATRIX 1 ; Dimension Matrix A as 2Γ2 f USER ; Enter User mode (optional) f MATRIX 1 ; Select Matrix A for input ; Enter elements row by row: 1 STO A ; aββ 2 STO A ; aββ 3 STO A ; aββ 4 STO A ; aββ ; Now compute determinant: RCL MATRIX A f MATRIX 7 ; Compute determinant ; Result: -2 (for this example)
This is more of a workflow than a programβthe HP-15C's matrix functions are built in.
Key matrix commands:
f MATRIX 7 β Determinant, f MATRIX 9 β Transpose
LBL .4 ; Initialize: X=xβ, Y=r STO 0 ; Store x in R0 Rβ STO 1 ; Store r in R1 RCL 0 ; Return initial x RTN LBL .5 ; Iterate: compute next x RCL 0 ; x 1 RCL 0 - ; (1 - x) Γ ; x(1-x) RCL 1 Γ ; rΒ·xΒ·(1-x) STO 0 ; Store new x RTN
Enter r ENTER xβ then GSB .4 to initialize.
Press GSB .5 repeatedly to iterate.
Try it: r=3.2, xβ=0.5 β Period-2 oscillation
Then try: r=3.57 β Edge of chaos, r=4.0 β Full chaos
LBL .6 ; R0=principal, R1=rate, R2=deposit, R3=periods ; Setup: Store values first, then GSB .6 RCL 3 ; n periods STO 4 ; Loop counter RCL 0 ; Starting principal LBL .7 ; Compound loop RCL 1 1 + ; 1 + rate Γ ; balance Γ (1 + rate) RCL 2 + ; + deposit 1 STO- 4 RCL 4 xβ 0? GTO .7 RTN ; Final balance in X
Example: $10,000 initial, 0.5% monthly rate, $500/month, 30 years:
10000 STO 0, .005 STO 1, 500 STO 2, 360 STO 3, GSB .6
Result: β $502,257
LBL .8 ; Z=year, Y=month, X=day STO 0 ; day Rβ STO 1 ; month Rβ STO 2 ; year RCL 1 2 xβ€y? GTO .9 RCL 1 12 + STO 1 ; Adjust month (Jan=13, Feb=14) 1 STO- 2 ; Adjust year LBL .9 RCL 0 ; d RCL 1 1 + 13 Γ 5 Γ· INT + ; + floor(13(m+1)/5) RCL 2 + ; + K RCL 2 4 Γ· INT + ; + floor(K/4) RCL 2 100 Γ· INT STO 3 ; J = century 4 Γ· INT + ; + floor(J/4) RCL 3 2 Γ - ; - 2J 7 Γ· FRAC 7 Γ INT ; mod 7 RTN
Enter year ENTER month ENTER day, press GSB .8.
Result: 0=Sat, 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri
Example: 2024 ENTER 7 ENTER 4 β July 4, 2024 was a Thursday (5)
LBL A ; n in X (setup for gamma) STO 4 ; Store n RTN ; Return to setup LBL B ; Integrand: t^n Γ e^(-t) ; t is in X from integrate RCL 4 ; n y^x ; t^n LastX CHS e^x ; e^(-t) Γ RTN ; To use: ; 1. Enter n, GSB A (stores n) ; 2. Enter: 0 ENTER 20 ; 3. f β« B (integrate label B) ; Result: Ξ(n+1) = n!
Enter n, GSB A (stores n)
Enter 0 ENTER 20 (integration limits)
Press f β« B (integrate label B)
Example: n=0.5 gives Ξ(1.5) = 0.5! β 0.886 = βΟ/2