- #1
brushman
- 113
- 1
Homework Statement
Write a function, is_prime, which takes a single integral argument and returns True when the argument is a prime number and False otherwise. Add doctests to your function as you develop it.
2. The attempt at a solution
Code:
def is_prime(n):
x = 1
while x<=n:
x+=1
if n%x==0:
return False
elif x==n:
return True
if __name__ == '__main__':
import doctest
doctest.testmod()
I figure x goes from 2 to n, and if it finds a factor it returns false; otherwise, true. Why doesn't this work?