
#Fibonacci sequence code python code
The code in this question is for the recursive approach.
#Fibonacci sequence code python series
With regard to space complexity, should I consider the space used as an arithmetic series because the list would have to be moved elsewhere if the number entered is larger than what is given to the function stack at the beginning? num int (input ()) def fibonacci (n): if n < 1: return 0 else: x, y 0, 1 for i in range (n): print (x) x, y y, x+y fibonacci (num) I used the original commentor code but I change it up so that you could print 0 as initially it was calculating before printing that is why, it doesnt print 0. Here, also Space Optimisation Taking 1st two Fibonacci numbers as 0 and 1. If it is TRUE, the function returns the value Zero. The append method takes O(1) time as shown here but I am doing the operation almost n times so would I get O(n) for time complexity? Python Program for Fibonacci numbers using Recursion. Python Fibonacci Series program using Recursion if (num 0) checks whether the given number is 0 or not.

The code is in Python and I wrote it such that I use a list and append to it and just return the last value. This means to say the nth term is the sum of (n-1)th and (n-2)th term. All other terms are obtained by adding the preceding two terms. This is not the most efficient way to get Fibonacci sequence numbers but I am learning Big O and would like confirmation and explanation of the space and time efficiency of the code below. python python-3.x fibonacci-sequence Share Improve this question Follow edited at 4:08 S On 26.6k 14 37 170 asked at 0:44 Malemna 11 2 1 What is it supposed to do At the very very least, show the result of the fibonaccicalc (19,0, 7) call you already do. A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8.
