Skip to content

Instantly share code, notes, and snippets.

@johannes-scharlach
Last active August 29, 2015 14:01
Show Gist options
  • Save johannes-scharlach/6fc65eab5ee41cfa7c38 to your computer and use it in GitHub Desktop.
Save johannes-scharlach/6fc65eab5ee41cfa7c38 to your computer and use it in GitHub Desktop.
Python Fibonacci function
def fibonacci(n, a=-1, b=1):
return [a, b] if n==0 else fibonacci(n+(n<0)-(n>0), b-a*(n<0), a+b*(n>0))
@johannes-scharlach
Copy link
Author

Short Python implementation of the fibonacci numbers for positive and negative n. Usage to get the Fibonacci number of any (positive or negative) n:

fibonacci_of_n = sum(fibonacci(n))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment