Last active
August 29, 2015 13:56
-
-
Save derektamsen/1c14ca576b2fe075bfc1 to your computer and use it in GitHub Desktop.
Produces the nth position of the Fibonacci Squence
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
#!/usr/bin/env python | |
# Call this script with a number after the script name and it will produce the nth position in the Fibonacci sequence. | |
# http://www.math.rutgers.edu/~erowland/fibonacci.html | |
# function fib(n) => return the nth number in fibonacci sequence | |
# 0, 1, 1, 2, 3, 5, 8 | |
# fib(1) => 0 | |
# fib(2) => 1 | |
# fib(3) => 2 | |
# fib(4) => 3 | |
# fib(5) => 5 | |
# fib(6) => 8 | |
import math | |
import sys | |
def calc_fib(position): | |
result = (math.pow((1 + math.sqrt(5)), position) - math.pow((1 - math.sqrt(5)), position)) / (math.pow(2, position) * math.sqrt(5)) | |
return int(result) | |
def main(): | |
print calc_fib(int(sys.argv[1])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment