Showing posts with label number theory. Show all posts
Showing posts with label number theory. Show all posts

Sunday, September 14, 2025

RPN HP 12C: Fibonacci and Lucas Sequences

 RPN HP 12C: Fibonacci and Lucas Sequences



Golden Ratio, Formulas, and Sequences


Let φ be the Golden Ratio:

φ = (1 + √5) ÷ 2


Formulas and Sequences


Fibonacci Numbers:

Fn = (φ^n - (-φ)^-n) ÷ √5


F0 = 0

F1 = 1

F2 = 1

F3 = 2

F4 = 3

F5 = 5

F6 = 8

F7 = 13

F8 = 21

F9 = 34

F10 = 55


Lucas Numbers:

Ln = φ^n + (-1)^n × (1 ÷ φ^n)


P0 = 2

P1 = 1

P2 = 3

P3 = 4

P4 = 7

P5 = 11

P6 = 18

P7 = 29

P8 = 47

P9 = 76

P10 = 123


HP 12C Code: Fibonacci and Lucas Numbers


Stack: X: n


01:[    44, 1]: STO 1

02:[        1]: 1

03:[    44, 0]: STO 0

04:[        5]: 5

05:[   43, 21]: √

06:[44, 40, 0]: STO+ 0

07:[        2]: 2

08:[44, 10, 0]: STO÷ 0 // Golden Ratio


09:[    45, 0]: RCL 0

10:[    45, 1]: RCL 1

11:[       21]: y^x

12:[    44, 2]: STO 2

13:[    44, 3]: STO 3

14:[    45, 0]: RCL 0

15:[       16]: CHS

16:[    45, 1]: RCL 1

17:[       16]: CHS

18:[       21]: y^x

19:[44, 30, 2]: STO- 2

20:[        5]: 5

21:[   43, 21]: √

22:[44, 10, 2]: STO÷ 2

23:[    45, 2]: RCL 2

24:[       31]: R/S // Fibonacci Numbers


25:[        1]: 1

26:[       16]: CHS

27:[    45, 1]: RCL 1

28:[       21]: y^x

29:[    45, 3]: RCL 3

30:[       22]: 1/x

31:[       20]: ×

32:[44, 40, 3]: STO+ 3

33:[    45, 3]: RCL 3

34:[43, 33, 00]: GTO 00



Variables:

R0 = φ

R1 = n

R2 = Fn

R3 = Ln


Sources


"Generalizations of Fibonacci numbers". Wikipedia. https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Fibonacci_integer_sequences Last Edited October 6, 2024. Accessed June 18, 2025


Olson, Scott. The Golden Section: Nature Greatest Secret. Wooden Books. Bloomsbury. New York, NY and London, UK. 2006. ISBN 978-0-8027-1539-5



Eddie


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

The author does not use AI engines and never will.

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.

Saturday, March 30, 2024

HP Prime and Casio fx-CG 50: Dedekind Sums

 HP Prime and Casio fx-CG 50: Dedekind Sums


Definition


The Dedekind Sum is defined as follows:


Let P and Q be relatively prime integers, that is GCD(P, Q) = 1.


Then S is the Dedekind sum as:


S = Σ( ((I ÷ Q)) × ((P × I ÷ Q)), for I=1 to Q)


The double parenthesis around the terms I ÷ Q and P × I ÷ Q signify a custom function:


(( X )) =

0, if X is an integer

X – FLOOR(X) – 1/2, if X is not an integer


If X is positive, X – INTG(X) – 1/2


HP Prime: DEDEKIND

EXPORT DEDEKIND(p,q)

BEGIN

// 2024-02-21 EWS

LOCAL s,i,a,b;



// Calculation

IF CAS.gcd(p,q)==1 THEN

s:=0;



FOR i FROM 1 TO q DO



a:=i/q;

IF FP(a)==0 THEN

a:=0;

ELSE

a:=a-FLOOR(a)-0.5;

END;



b:=p*i/q;

IF FP(b)==0 THEN

b:=0;

ELSE

b:=b-FLOOR(b)-0.5;

END;

s:=s+a*b;

END;

RETURN s;

ELSE

RETURN "p and q are not relatively prime.";

END;

END;


Casio fx-CG 50: DEDEKIND

244 bytes


Code:

 “DEDEKIND SUM: S(P,Q)”

“P”? → P

“Q”? → Q


If GCD(P,Q)≠1

Then

“P AND Q ARE NOT RELATIVELY PRIME”

Stop


For 1→ I To Q

I÷Q → A

Frac A=0 ⇒ 0 → A

Frac A≠0 ⇒ A – Intg A – 0.5 → A

P × I ÷ Q → B

Frac B=0 ⇒ 0 → B

Frac B≠0 ⇒ B – Intg B – 0.5 → B
S + A × B → S

Next

S


Note: The are 6 spaces between NOT and RELATIVELY to align the text.


Examples


P

Q

Results (fraction)

Results (decimal)

2

17

8/17

0.4705882353

1

21

95/63

1.50793650794

9

43

27/86

0.3139534884

8

67

53/134

0.3955223881

4

75

649/450

1.442222222

14

57

-140/171

-0.8187134503


Sources

Shipp, R. Dale. “Table of Dedekind Sums” Journal of Research of the National Bureau of Standards-B. Mathematics and Mathematical Physics Vol. 69B, No 4, October-December 1965 https://nvlpubs.nist.gov/nistpubs/jres/69B/jresv69Bn4p259_A1b.pdf

Retrieved February 21, 2024


Weisstein, Eric W. "Dedekind Sum." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/DedekindSum.html

Retrieved February 18, 2024


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.

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026 For my review on the Sharp EL-5200 (also known as the Sharp EL-9000)...