Created
June 1, 2015 09:23
-
-
Save buranmert/ee27ff4dfa6c40593974 to your computer and use it in GitHub Desktop.
Fast Fibonacci algorithm
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
def fibonacci(n): | |
if n <= 2: | |
return 1 | |
elif n <= 0: | |
return 0 | |
if n % 2 == 0: | |
x = n/2 | |
return fibonacci(x) * (fibonacci(x + 1) + fibonacci(x - 1)) | |
else: | |
x = (n-1)/2 | |
return fibonacci(x+1)**2 + fibonacci(x)**2 | |
if __name__ == "__main__": | |
while True: | |
print "Enter number:" | |
input = int(raw_input()) | |
if input < 0: | |
break | |
print "Processing..." | |
print "Result: " + str(fibonacci(input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment