Last active
March 9, 2018 10:16
-
-
Save bravikov/91d054f97fb20b8384ad9c4ad4e10e36 to your computer and use it in GitHub Desktop.
Fibonacci number
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
# run: python3 fibonacci.py | |
def fibonacci(n): | |
if n == 0: | |
return 0 | |
if n < 0: | |
return int(-1**n) * fibonacci(-n) | |
current = 1; | |
previous = 0; | |
for i in range(1, n): | |
temp = current | |
current += previous | |
previous = temp | |
return current | |
# Test | |
for i in range(-10, 10): | |
print(i, fibonacci(i)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment