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.