Write a program to generate the Fibonacci sequence up to n-th term


Topic: Write a program to generate the Fibonacci sequence up to n-th term

Solution

nterms = int(10000)
n1, n2 = 0, 1
count = 0
fab_list = []
if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   while count < nterms:
       #print(n1)
       fab_list.append(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1
fn = lambda x: "FIBONACCI" if x in fab_list else "NOT_FIBONACCI"
print("Given number is",fn(20))



List all Python Programs