# Fibonocci Numbers Fibonocci numbers is given by \begin{equation} F_n = F_{n-1} + F_{n-2}\tag{1} \end{equation} where \begin{equation} F_1 = F_2 = 1\tag{2} \end{equation} So the first few numbers are \begin{equation*} 1,\ 1,\ 2,\ 3,\ 5,\ 8,\ ... \end{equation*} What I done in this program is that entering a how many Fibonocci numbers you want, then I allocate the memeory for that array. Next is the simplest part, I iterate the Fibonocci number using (1), and write the result out. ## Code ```fortran !======================================================== ! Purpose: Find first N of Fibonacci numbers ! ! Methods: Fn = Fn-1 + Fn-2 ! ! Date Programer Description of change ! ==== ========= ===================== ! 9/28/20 Morris Original Code !======================================================== PROGRAM fibonacci IMPLICIT NONE INTEGER :: N, i INTEGER, PARAMETER :: verylong = selected_int_kind(32) INTEGER(verylong), DIMENSION(:), ALLOCATABLE :: fib write(*, *) "Numbers of Fibonocci number you want >>>" read(*, *) N ! allocate size N of array allocate(fib(N)) ! F1 = F2 = 1 fib(1) = 1 fib(2) = 1 do i = 3, N fib(i) = fib(i-1) + fib(i-2) end do ! write to a file open(1, file='fib.dat', status='unknown') write(1, *) fib ! write on screen write(*, '(a)') repeat("=", 100) write(*, '(10I10)') fib(1:10) write(*, '(a)') repeat("=", 100) write(*, '(10I10)') fib(11:20) write(*, '(a)') repeat("=", 100) write(*, '(10I10)') fib(21:30) write(*, '(a)') repeat("=", 100) write(*, '(10I10)') fib(31:40) write(*, '(a)') repeat("=", 100) END PROGRAM fibonacci ``` ## Result Below screenshot shows the first 40 Fibonocci number. ![](https://i.imgur.com/v133yga.png) Lastly, I plot the Fibonocci number vs. N. It clearly shows a extremly rapid growth. ![](https://i.imgur.com/zRpyIFb.png) In 64 bit Fortran integer type, there are limit to the maximun digit you can hold in one variable. In order to calculate 200th Fibonocci number, I need to seperate this very long integer($\approx$ 42 digits) into different place. What I do is assigning each digit to an element of array, so that I can store as big number as I desire. Hardest part is then to calculate the sum of two number which I store digit ny digit in two big array. Below program(line 30 to 36) show how I manage to calulate the sum of two number by using two array. ## Big Fibonocci Number ```fortran PROGRAM big_fibonacci IMPLICIT NONE INTEGER :: N, i, j, res=0 ! store 50 digit of number of 3 Fibonocci numbers INTEGER, DIMENSION(:, :), ALLOCATABLE :: fib write(*, *) "Numbers of Fibonocci number you want >>>" read(*, *) N ! allocate N x 50 array allocate(fib(N, 50)) ! initialize fib = 0. ! F1 = F2 = 1 fib(1, 50) = 1 fib(2, 50) = 1 CALL big_addition CONTAINS SUBROUTINE big_addition write(*, '(a)') repeat('=', 100) write(6, '(50I2, 7I7)') fib(1, :), 1 write(6, '(50I2, 7I7)') fib(2, :), 2 do i = 3, N res = 0 do j = 50, 1, -1 fib(i, j) = mod(fib(i-1, j) + & fib(i-2, j) + res, 10) res = (fib(i-1, j) + fib(i-2, j) + res & - mod(fib(i-1, j) + fib(i-2, j) & + res, 10)) / 10 end do write(6, '(50I2, 7I7)') fib(i, :), i end do write(*, '(a)') repeat('=', 100) ! write to a file open(1, file='f200.dat', status='unknown') do i = 1, N write(1, '(50I1)') fib(i, :) end do END SUBROUTINE big_addition END PROGRAM big_fibonacci ``` ### Result Below screenshot show the 190th to 200th Fibonocci number which I store each digit seperatly(there is no way I can calculate this big number just using intrinsic Fortran function). ![](https://i.imgur.com/mpoN1aB.png) # Golden Mean Golden mean is the ratio between two succescive Fibonocci numbers. \begin{equation} Golden \ Mean = \lim_{n \rightarrow \infty} \frac{F_{n}}{F_{n+1}} = \frac{\sqrt{5}+1}{2} = 0.618... \tag{3} \end{equation} ## Code ```fortran !======================================================== ! Purpose: Find Golden Mean ! ! Methods: Fn+1 / Fn ! ! Date Programer Description of change ! ==== ========= ===================== ! 9/28/20 Morris Original Code !======================================================== PROGRAM golden IMPLICIT NONE INTEGER :: N, i INTEGER(16), DIMENSION(:), ALLOCATABLE :: fib REAL(16), DIMENSION(:), ALLOCATABLE :: ratio REAL(16) :: golden write(*, *) "Numbers of Fibonocci number you want >>>" read(*, *) N ! allocate N x 50 array allocate(fib(N)) allocate(ratio(N-1)) ! initialize fib = 0. ! F1 = F2 = 1 fib(1) = 1 fib(2) = 1 ratio(1) = dble(fib(2)) / dble(fib(1)) golden = (dsqrt(5.D0) - 1.D0) / 2.D0 write(*, '(a)') repeat('=', 100) write(6, '(I25, 7I7)') fib(1), 1 write(6, '(I25, 7I7)') fib(2), 2 do i = 3, N fib(i) = fib(i-1) + fib(i-2) ratio(i-1) = dble(fib(i-1)) / dble(fib(i)) write(6, '(I25, 7I7)') fib(i), i end do write(*, '(a)') repeat('=', 100) do i = 1, N-1 write(6, '(D30.20, D30.20, 7I7)') ratio(i), & abs(ratio(i) - golden), i end do write(*, '(a)') repeat('=', 100) ! write to a file open(1, file='golden.dat', status='unknown') do i = 1, N write(1, '(F30.20)') ratio(i) end do END PROGRAM golden ``` ## Result I can calculate two successive Fibonocci ratio and it converges to golden ratio very fast. ![](https://i.imgur.com/aCmatVd.png) If I calculate the error between the ratio I calculate and (3), the error decrease as N increase. I plot the N vs $\varepsilon$ in which y axis is log scale. For $\varepsilon < 10^{-12}$, at least I need to calcuate the ratio between 31th and 30th Fibonocci number. ![](https://i.imgur.com/S1sRdEn.png)