| Download
Jupyter notebook Fibonnacci_numbers.ipynb
Project: Recursive Fibonnaci
Path: Fibonnacci_numbers.ipynb
Views: 105Kernel: T - Python 3 (Ubuntu Linux)
The Task - create a program that prints the fibonacci numbers
Fibonacci numbers are a set of numbers where each sequential number is the sum of the previous two numbers... i.e. 1, 1, 2, 3, 5, 8, 13, etc.
I explore three different ways to code a solution.
The first solution shown below takes an input x
, and generates all fibonacci numbers between 1 and x
.
First solution - switching the values around
In [1]:
In [2]:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Streamlined version:
In [3]:
In [4]:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
What if we wanted to produce a set number of fibonacci numbers, as opposed to all the fibo numbers up to a particular value?
Second solution - set number of iterations:
In [5]:
In [6]:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
Third solution - recursive:
In [1]:
In [8]:
6765
I would like to use the above recursive function to print x iterations, so I will define a separate function to do so:
In [9]:
In [10]:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
And finally...
I wish to make the output slightly more practical for the user. To clarify what the nth value is, I have slightly altered the FiboPrint function:
In [11]:
In [12]:
Iteration 1 is: 1
Iteration 2 is: 1
Iteration 3 is: 2
Iteration 4 is: 3
Iteration 5 is: 5
Iteration 6 is: 8
Iteration 7 is: 13
Iteration 8 is: 21
Iteration 9 is: 34
Iteration 10 is: 55