Skip to content

Instantly share code, notes, and snippets.

@georgemarshall
Created March 18, 2011 09:05
Show Gist options
  • Save georgemarshall/875797 to your computer and use it in GitHub Desktop.
Save georgemarshall/875797 to your computer and use it in GitHub Desktop.
fibonacci index lookup and prime tester with doctest
def fibonacci(n):
"""Return value at n in fibonacci sequence
>>> fibonacci(3)
3
>>> fibonacci(6)
13
>>> fibonacci(9)
55
"""
x, y = 1, 1
for i in xrange(n):
x, y = y, x + y
return x
def prime(n):
"""Return True if n is prime
>>> prime(2)
False
>>> prime(7)
True
>>> prime(64)
False
"""
if not n % 2:
return False
for i in xrange(3, n, 2):
if not n % i:
return False
return True
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment