05.fibonacci sequence
Fibonacci Sequence
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....
The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.
n = int(input("How many terms do you want? "))
## Initializing first two terms
n1 = 0
n2 = 1
count = 0
if n <= 0 :
print('Please enter a positive number')
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
n = int(input("How many terms do you want? "))
## Initializing first two terms
n1 = 0
n2 = 1
count = 0
if n <= 0 :
print('Please enter a positive number')
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
n1, n2 = n2, n1+n2
count += 1
def fibonacci (n):
n1 = 0
n2 = 1
count = 0
if n <= 0 :
print('Please provide a positive number')
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
n1, n2 = n2, n1+n2
count += 1
fibonacci(4)