A series of Fibonacci numbers is a sequence. The first and second elements of the sequence are equal to one. Each subsequent element is equal to the sum of the previous two. Let’s look at different ways of finding elements by number and generating a list using Python 3.
Calculating Fibonacci number series is one of the best examples of Python programs that use recursion. Although the most common example, recursion is the calculation of a factorial. Let’s look at the options for getting Fibonacci series in Python 3:
- Using recursion.
- Using the loop operator.
Let’s also generate a list of numbers and create a generator with which we can get numbers one by one.
We will search using the for loop. The variables
cur will contain the previous element of the sequence and the current one and we will initialize them to 1. If the user asks for the first or second element, we will never get to the loop’s body. And the one from
cur variable will be output. If they ask for the third or any subsequent element of the Fibonacci sequence, we will get into the loop. We will save the next number of the sequence to the temporary variable
tmp. After that we will fill
cur with new values. When the required number of iterations has passed, we will output
cur to the console.
prew = cur = 1 element = input('Enter the number of the element you are looking for : ') element = int(element) for n in range(int(element-2)) tmp = prew + cur prew = cur cur = tmp print(str(element)+ 'sequence element is ' + str(cur)) Enter the number of the element you are looking for : 6 6 sequence element equals 8
In the previous code, we had to use the
tmp variable. But we can rewrite the code inside the loop as follows:
prew, cur = cur, prew + cur
Now we have one line of code instead of three! And we don’t need to use an extra variable anymore. In this example we used the
for loop, but we can implement this program by changing the code a little bit, using the
For recursion, we will write a function with the desired number of the Fibonacci series as an argument. Let’s first assign 1 to the current value of
cur sequence. After that, we will use the conditional Python operator –
if. In it we will check the argument of the function. If it is greater than 2, the function will call itself and calculate the previous value of the series and the value that was before and write their sum into the
def fibonacci(n): cur = 1 if n > 2: cur = fibonacci(n-1) + fibonacci(n-2) return cur element = input('Enter the number of the element you are looking for : ') element = int(element) value = fibonacci(element) print(str(element)+ ''sequence element is ' + str(value))
Of course, the recursion example is interesting. But it will be much slower. And if you decide to calculate, say, the 1000th element in a sequence. We will compute it very quickly using a loop. But in case of recursion we will get an error of exceeding the maximum number of recursions:
RecursionError: maximum recursion depth exceeded in comparison
The list generator
If we want to initialize our list with a Fibonacci number we can do it this way
def fibonacci(n): a, b = 1, 1 for i in range(n): yield a a, b = b, a + b data = list(fibonacci(10)) print(data) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
fibonacci(10) is the generator of a row object of dimension 10. With each subsequent call it will return another element with yield. We create a list from it. Then we output the list to the console using the
def fibonacci(): a, b = 1, 1 while True: yield a a, b = b, a + b gen = fibonacci() for i in range(5): print(next(gen)) 1 1 2 3 5
Here we have created a Fibonacci number generator using Python 3. With the
next function we get the numbers of the series one by one.