1#Python program to generate Fibonacci series until 'n' value
2n = int(input("Enter the value of 'n': "))
3a = 0
4b = 1
5sum = 0
6count = 1
7print("Fibonacci Series: ", end = " ")
8while(count <= n):
9 print(sum, end = " ")
10 count += 1
11 a = b
12 b = sum
13 sum = a + b
14
1# By recursion
2def fib(n):
3 if n == 1 or n == 2:
4 return 1
5 else:
6 return(fib(n-1) + fib(n-2))
7
8n = 6
9for i in range(1,n+1):
10 print(fib(i))
1# WARNING: this program assumes the
2# fibonacci sequence starts at 1
3def fib(num):
4 """return the number at index num in the fibonacci sequence"""
5 if num <= 2:
6 return 1
7 return fib(num - 1) + fib(num - 2)
8
9
10print(fib(6)) # 8
1#fibonacci sequence with memory to increase the speed.
2class recur_fibo:
3 memory = {0: 1, 1:1}
4
5 def fibonacci(n):
6 if n in recur_fibo.memory:
7 return recur_fibo.memory[n]
8 else:
9 recur_fibo.memory[n] = recur_fibo.fibonacci(n-1) + recur_fibo.fibonacci(n-2)
10 return recur_fibo.memory[n]
11
12if __name__ == "__main__":
13 value = recur_fibo.fibonacci(200)
14 print(value)
1#Learnprogramo
2Number = int(input("How many terms? "))
3# first two terms
4First_Value, Second_Value = 0, 1
5i = 0
6if Number <= 0:
7print("Please enter a positive integer")
8elif Number == 1:
9print("Fibonacci sequence upto",Number,":")
10print(First_Value)
11else:
12print("Fibonacci sequence:")
13while i < Number:
14print(First_Value)
15Next = First_Value + Second_Value
16# update values
17First_Value = Second_Value
18Second_Value = Next
19i += 1