The Fibonacci sequence is the infinite sequence of natural numbers, which begins with twice the number 1 or, often in modern notation, is additionally provided with a leading number 0. In the further sequence of numbers, the sum of two consecutive numbers results in the number immediately following it.
def fibonacciAlgorithm(n):
if n == 0:
print(0)
return
first, second = 0, 1
print(repr(first) + " " + repr(second), end=" ")
for i in range(3, n):
third = first + second
print(repr(third), end=" ")
first = second
second = third
n = int(input("Enter the number of Fibonaci numbers to be generated: "))
fibonacciAlgorithm(n)
Enter the number of Fibonaci numbers to be generated: 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
def fibonacciAlgorithm(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacciAlgorithm(n - 1) + fibonacciAlgorithm(n - 2)
n = int(input("Enter n-th number of fibonacci sequence: "))
print(fibonacciAlgorithm(n))
Enter n-th number of fibonacci sequence: 12
144