Skip to content

Instantly share code, notes, and snippets.

@fractalbach
Created October 23, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fractalbach/d4769fb805ba61b6539bb49a487c231a to your computer and use it in GitHub Desktop.
Save fractalbach/d4769fb805ba61b6539bb49a487c231a to your computer and use it in GitHub Desktop.
Fibonacci number in closed form expression
import sys
import math
def fib(n):
a = (1 + 5**(1/2)) / 2
b = (1 - 5**(1/2)) / 2
return (a**n - b**n)/ 5**(1/2)
if len(sys.argv) is not 2:
print("need 1 argument: a number for fib(n).")
exit(1)
s = sys.argv[1]
n = int(s)
print(round(fib(n)))
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment