Showing posts with label Fibonacci numbers. Show all posts
Showing posts with label Fibonacci numbers. 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.

Thursday, December 20, 2018

HP Prime and TI-84 Plus: Fibonacci Triangles (updated)

HP Prime and TI-84 Plus:  Fibonacci Triangles

This program is the request of John Cvetan.  I thank you for your suggestion.

Introduction

The program FIBMAT generates the Fibonacci Triangle in matrix form.  The Fibonacci triangle is a triangle generated where the outer entries of each row contain the Fibonacci sequence.  The Fibonacci sequence is generated by:

f_0 = 1
f_ 1 = 2
f_n = f_n-1 + f_n-2

You can quickly calculate the nth Fibonacci number by the formula:

f_n = ( (1 + √5)^n - (1 - √5)^n ) / (2^n * √5)

To generate the Fibonacci triangle,

1.  Let r the row and c be the column with

f_0,0 =  1
f_1,0 = 1
f_1,1 = 1
f_2,1 = 1

2.  Each row will be determined by adding the last two terms going diagonally.  You can use one of two formulas:

f_r,c = f_r-1,c + f_r-2,c

f_r,c = f_r-1,c-1 + f_r-1,c-2

The Program FIBMAT

FIBMAT generates a Fibonacci triangle in matrix form.  It's the result is a triangle that is "tilted".  n will need to be 3 or greater.



HP Prime Program FIBMAT

EXPORT FIBMAT(n)
BEGIN
// Fibonacci "triangle" in
// matrix form
// 2018-12-17 EWS
LOCAL M1,k;
M1:=MAKEMAT(0,n+1,n+1);
M1(1,1):=1;
M1(2,1):=1;
M1(2,2):=1;
FOR k FROM 3 TO n+1 DO
M1(k):=row(M1,k-1)+row(M1,k-2);
M1(k,k):=M1(k-1,k-1)+M1(k-2,k-2);
END;
RETURN M1;
END;

Here's an alternate code for Fibonacci Triangle Matrices.  The row command has been eliminated and I used a second For loop in its place.  (added 12/23/2018)

EXPORT FIBMATALT(n)
BEGIN
// 2018-12-23 EWS
// Fibonacci Matrix Alternate
// matrix form
// This version does not have the
// row function.
LOCAL M1,k,j;
M1:=MAKEMAT(0,n+1,n+1);
M1(1,1):=1;
M1(2,1):=1;
M1(2,2):=1;
FOR k FROM 3 TO n+1 DO
FOR j FROM 1 TO n DO
M1(k,j):=M1(k-2,j)+M1(k-1,j);
END;
M1(k,k):=M1(k-1,k-1)+M1(k-2,k-2);
END;
RETURN M1;
END;


TI-84 Plus Program FIBMAT

"2018-12-18 EWS"
"FIBONACCI MATRIX"
Input "ORDER: ",N
{N+1,N+1}→dim([A])
1→[A](1,1)
1→[A](2,1)
1→[A](2,2)
For(K,3,N+1)
For(J,1,N)
[A](K-2,J)+[A](K-1,J)→[A](K,J)
End
[A](K-1,K-1)+[A](K-2,K-2)→[A](K,K)
End
Pause [A]

The Program FIBTRI

This is a visual program for Fibonacci Triangle.



FIBTRI(n) generates a visual Fibonacci Triangle - although I don't recommend going beyond 12 rows due to the constraints of the screen.  I used the small font for the rows.

HP Prime Program FIBTRI

EXPORT FIBTRI(n)
BEGIN
// Fibonacci triangle
// 2018-12-17 EWS
LOCAL M1,k;
M1:=MAKEMAT(0,n+1,n+1);
M1(1,1):=1;
M1(2,1):=1;
M1(2,2):=1;
FOR k FROM 3 TO n+1 DO
M1(k):=row(M1,k-1)+row(M1,k-2);
M1(k,k):=M1(k-1,k-1)+M1(k-2,k-2);
END;

RECT();
LOCAL s;
FOR k FROM 1 TO n+1 DO
s:=STRING(SUB(row(M1,k),1,k));

IF k≤6 THEN
TEXTOUT_P(s,
140-5.5*(k-1),(k-1)*15,2);
END;

IF k>6 AND k≤11 THEN
TEXTOUT_P(s,
140-8*(k-1),(k-1)*15,2);
END;

IF k>11 THEN
TEXTOUT_P(s,
140-11.5*(k-1),(k-1)*15,2);
END;

END;
WAIT(0);
END;

Source:

Hosoya, Haruo.  "Fibonacci Triangle"  Ochanomizu University, Tokyo, Japan.  1976.  https://www.fq.math.ca/Scanned/14-2/hosoya.pdf

Eddie

All original content copyright, © 2011-2018.  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.  Please contact the author if you have questions.

Monday, May 15, 2017

HP 10C and HP 11C: Fibonacci Numbers (Tom Grydeland)

HP 10C and HP 11C: Fibonacci Numbers (Tom Grydeland)

Hi!  It’s good to be blogging again.  My presentation for HHC 2017 is almost ready and I will be reviewing and polishing it in June. 

Today’s program is courtesy of Tom Grydeland.  This is his Fibonacci number program for the HP-10C and HP-11C.  In the 1980s, Hewlett Packard sold a family of landscape calculators, the most famous being the HP-15C and HP-12C.  The HP-10C and HP-11C were simpler versions of the HP-15C. 

For the HP-15C version, check out this post:  http://edspi31415.blogspot.com/2017/03/hp-15c-fibonacci-numbers.html

I thank you Tom for letting me post this on my blog.

Here is what Tom wrote to me:

Dear sir,

In response to your blog post on Fibonacci numbers for the HP-15C:

For the HP-11C, the DSE instruction (and ISG also) references register
I only.  Therefore, to run Joe Horn's program on the 11C, replace step
002 (STO 0) with '44, 25' (STO I), and step 010 (DSE 0) with '42, 5'
(DSE).

For the HP-10C, there are no ISG or DSE instructions, nor labels. The
program becomes (tested)

Step    Key Code        Mnemonic
001     1               (1)
002     34              (X<>Y)
003     0               (0)
004     34              (X<>Y)
005     1               (0)
006     30              (-)
007     44 0            (STO 0)
008     33              (Rv)
009     40              (+)
010     42 36           (LST X)
011     34              (X<>Y)
012     45 0            (RCL 0)
013     42 20           (X=0?)
014     22 16           (GTO 16)
015     22 05           (GTO 05)
016     33              (Rv)
017     22 00           (GTO 00 == STOP)

thank you and best regards.


Eddie



This blog is property of Edward Shore, 2017.

Wednesday, March 22, 2017

HP 15C: Fibonacci Numbers


HP 15C: Fibonacci Numbers (Updated, bug with HP 15C LE?)
 (edited 3/23/2017)

Approximation Formula
This program calculates the nth Fibonacci number.  Store n in memory register 0 before running the program.

Formula:  ( (1 + √5)^n – (1 - √5)^n ) / (2^n * √5)

Caution:  Due to internal calculator calculation, you may not get an integer answer.  You might have to round the answer manually.   From Joe Horn, with thanks and appreciation for letting me post his comment:  

"Hi, Eddie! I just keyed up your HP-15C program for Fibonacci numbers, and noticed that it gets wrong answers due to round-off errors much of the time. For example, with an input of 5, it should output 5, but it outputs 4.9999999998. Even rounding to the nearest integer doesn't fix the problem for inputs of 40 through 49. The only way to get the right answers for all inputs is with a loop (the usual method)." - Joe Horn

 Therefore, I would recommend using this program for n < 40.  You may need to round results to the nearest integer.
Step
Key
Code
001
LBL D
42, 21, 14
002
1
1
003
ENTER
36
004
5
5
005
√
11
006
+
40
007
RCL 0
45, 0
008
Y^X
14
009
1
1
010
ENTER
36
011
5
5
012
√
11
013
-
30
014
RCL 0
45, 0
015
Y^X
14
016
-
30
017
2
2
018
RCL 0
45, 0
019
Y^X
14
020
÷
10
021
5
5
022
√
11
023
÷
10
024
RTN
43, 32

Example:  R0 = n = 16, Output: 987

Loop Method – Joe Horn

Joe Horn provided a more accurate program which uses loops.  It is slower, however should speed should not be a problem if a HP 15C Limited Edition or emulator is used.  Full credit and thanks goes to Joe Horn for providing this program.  

Step
Key
Code
001
LBL A
42, 21, 11
002
STO 0
44, 0
003
1
1
004
ENTER
36
005
0
0
006
LBL 1
42, 21, 1
007
+
40
008
LST X
43, 36
009
X<>Y
34
010
DSE 0
42, 5, 0
011
GTO 1
22, 1
012
RTN
43, 32

A nice part is that you don’t have to pre-store n in memory 0.   This method is accurate for n ≤ 49. 

Comparing Results
Here are some results of some selected n:

n
Approximation
Loop Method
6
8
8
15
610
610
16
987
987
22
17710.9999
17711
29
514228.9979
514229
36
14930351.92
14930352
40
102334154.4
102334155
44
701408728.7
701408733
49
7778741992
7778742049


Bug?

I manually calculated the approximation formula on my HP 15C LE (Limited Edition) and HP Prime for n = 44 and n = 49.

n = 44
HP15C LE: 701408728.7
HP Prime: 701408733.002

n = 49
HP 15C LE: 7778741992
HP Prime: 7778742049.02

I think we may have found a bug on the HP 15C LE.

Thanks to Joe Horn for the comments, much appreciated.
This blog is property of Edward Shore, 2017.



HP 15C: Fibonacci Numbers (Updated, bug with HP 15C LE?)

HP 15C: The I Squared Rule and HHC 2026

HP 15C: The I Squared Rule and HHC 2026 HHC 2026 Videos Before we start, the videos for HHC 2026 are up! Please take a look, ev...