Last active
November 30, 2015 04:36
-
-
Save harishvc/4e012526064445ebe927 to your computer and use it in GitHub Desktop.
Find the first n number of the Fibonacci series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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