Created
October 23, 2018 21:38
-
-
Save fractalbach/d4769fb805ba61b6539bb49a487c231a to your computer and use it in GitHub Desktop.
Fibonacci number in closed form expression
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
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