Skip to content

Instantly share code, notes, and snippets.

@derektamsen
Last active August 29, 2015 13:56
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 derektamsen/1c14ca576b2fe075bfc1 to your computer and use it in GitHub Desktop.
Save derektamsen/1c14ca576b2fe075bfc1 to your computer and use it in GitHub Desktop.
Produces the nth position of the Fibonacci Squence
#!/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