-
Notifications
You must be signed in to change notification settings - Fork 3
/
prime.py
40 lines (35 loc) · 850 Bytes
/
prime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!usr/bin/python
"""
Find the next prime number until the user chooses to stok asking for the next
one
"""
from math import sqrt
def isPrime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for divisor in range(3, int(sqrt(number)+1), 2):
if number % divisor == 0:
return False
return True
return False
def gen_primes():
yield 2
number = 3
while(True):
if isPrime(number):
yield number
number += 2
def main():
print "Press Enter to generate next prime number, 's' to stop."
generator = gen_primes()
while True:
ans = raw_input()
if ans == 's':
break
else:
print generator.next()
if __name__ == '__main__':
main()