-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathnext_prime.py
61 lines (40 loc) · 985 Bytes
/
next_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# Next Prime Number
# Generate prime numbers until
# the user chooses to stop
def isPrime(x):
"""
Checks whether the given
number x is prime or not
"""
if x == 2:
return True
if x % 2 == 0:
return False
for i in range(3, int(x**0.5)+1, 2):
if x % i == 0:
return False
return True
def genPrime(currentPrime):
"""
Returns the next prime
after the currentPrime
"""
newPrime = currentPrime + 1
while True:
if not isPrime(newPrime):
newPrime += 1
else:
break
return newPrime
def main(): # Wrapper function
currentPrime = 2
while True:
answer = input('Would you like to see the next prime? (Y/N) ')
if answer.lower().startswith('y'):
print(currentPrime)
currentPrime = genPrime(currentPrime)
else:
break
if __name__ == '__main__':
main()