Skip to content

Instantly share code, notes, and snippets.

@buranmert
Created June 1, 2015 09:23
Show Gist options
  • Save buranmert/ee27ff4dfa6c40593974 to your computer and use it in GitHub Desktop.
Save buranmert/ee27ff4dfa6c40593974 to your computer and use it in GitHub Desktop.
Fast Fibonacci algorithm
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