Created
June 24, 2016 19:29
-
-
Save alothings/85002055b221584fac051b97e8824474 to your computer and use it in GitHub Desktop.
Give an integer n, find the next prime
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
__author__ = "Alonso Gutierrez" | |
# Give input integer n, find the next prime | |
def find_next_prime(n): | |
isprime = False | |
n += 1 | |
while (not isprime): | |
# print n | |
isprime = True | |
if n % 2 == 0: | |
# print 'n is even' | |
isprime = False | |
for i in range(3, n/2, 2): | |
if n % i == 0: | |
# print 'n in for loop' | |
isprime = False | |
n += 1 | |
return n-1 | |
# Test cases | |
print find_next_prime(6) | |
print find_next_prime(9) | |
print find_next_prime(15) | |
print find_next_prime(788) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment