Skip to content

Instantly share code, notes, and snippets.

@MohanSha
Created February 13, 2018 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohanSha/c668dd36e43445487909d0fd475a5411 to your computer and use it in GitHub Desktop.
Save MohanSha/c668dd36e43445487909d0fd475a5411 to your computer and use it in GitHub Desktop.
Have the program find prime numbers until the user chooses to stop asking for the next one.
# Next Prime Number
# Have the program find prime numbers until the user chooses to stop asking for the next one.
import math
def is_prime(num):
if num < 2:
return False
for i in range(2, int(math.sqrt(num))+1):
if num % i == 0:
return False
return True
def get_next_prime(num):
i = num + 1
while True:
if is_prime(i):
return i
i += 1
if __name__ == "__main__":
print "Press enter to continue, done to stop"
i = 2
while True:
print i,
user_input = raw_input()
if user_input == 'done':
break
i = get_next_prime(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment