Created
February 13, 2018 07:52
-
-
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.
This file contains 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
# 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