Created
March 18, 2011 09:05
-
-
Save georgemarshall/875797 to your computer and use it in GitHub Desktop.
fibonacci index lookup and prime tester with doctest
This file contains hidden or 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
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