0
Print a Fibonacci series till specific range
mandatory to print error if range is smaller than starting number
3 RĂ©ponses
0
def fibo(n):
if n==1:
print ([0])
return
if n==2:
print ([0,1])
return
else:
l = [0,1]
for i in range(n-2):
l.append(l[-1] + l[-2])
print(l)
fibo(3)
0
You can nth Fibonacci number:
def fibo(n):
if n==1:
return 0
if n==2:
return 1
else:
l = [0,1]
for i in range(n-2):
l.append(l[-1] + l[-2])
print(l[-1])
fibo(6)
==================================
def fib(n):
a, b = 0, 1
if n == 1:
return 0
if n == 2:
return 1
else:
n -= 2
while n:
a, b = b, a+b
n = n -1
return b
print(fib(6))