Skip to content

Instantly share code, notes, and snippets.

@harishvc
Last active November 30, 2015 04:36
Show Gist options
  • Save harishvc/4e012526064445ebe927 to your computer and use it in GitHub Desktop.
Save harishvc/4e012526064445ebe927 to your computer and use it in GitHub Desktop.
Find the first n number of the Fibonacci series
#Reference: http://pythoncentral.io/python-generators-and-yield-keyword/
#Find the first n number of the Fibonacci series
def fibonacci(n):
curr = 1
prev = 0
counter = 0
while counter < n:
#print("Generator invoked ..")
yield curr
prev, curr = curr, curr + prev
counter += 1
def example3():
num = 5
print("Print the first %d number of the Fibonacci series" % (num))
for x in fibonacci(num):
print(x, end=" ")
example3()
#output
Print the first 5 number of the Fibonacci series
1 1 2 3 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment