Skip to content

Instantly share code, notes, and snippets.

@ashokbharat
Created June 16, 2017 13:58
Show Gist options
  • Save ashokbharat/491ce7c30a235752252af5d2d7649a30 to your computer and use it in GitHub Desktop.
Save ashokbharat/491ce7c30a235752252af5d2d7649a30 to your computer and use it in GitHub Desktop.
Fibonacci
'''Write a program that asks the user how many Fibonnaci numbers to generate and then
generates them. Take this opportunity to think about how you can use functions.
Make sure to ask the user to enter the number of numbers in the sequence to generate'''
n = int(input("Enter the number of fibonacci sequence numbers to be generated"))
#Using Recursion
def generate_fib(num):
if(num==0):
return 0
elif(num==1):
return 1
else:
return generate_fib(num-1) + generate_fib(num-2)
for i in range(0,n):
print(generate_fib(i))
#print(c)
#Without using Recursion
prev=0
next=1
for i in range(0,n):
if(i==0):
print(0)
elif(i==1):
print(1)
else:
result = prev+next
prev=next
next=result
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment