Created
January 12, 2015 02:46
-
-
Save ChanChar/6cb957d6a313ba275b7c to your computer and use it in GitHub Desktop.
Next Prime Rmotr Assignment
This file contains hidden or 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
### Assignment ### | |
# | |
# Your assignment is to implement the | |
# following function: `find_next_prime`. | |
# As the name states, given the number `n` the | |
# function should return the next closest prime. | |
# | |
# Examples: | |
# * `find_next_prime(6)` should return 7. | |
# * `find_next_prime(10)` should return 11. | |
# * `find_next_prime(11)` should return 13. | |
# | |
# You can use whatever you want (data structures, | |
# language features, etc). | |
# | |
# Unit tests would be a plus. Actually, just knowing what | |
# they are would be a plus :) | |
# | |
### End Assignment ### | |
def is_prime(n): | |
n = abs(n) | |
prime_number = True | |
if n == 0: | |
return prime_number | |
elif n <= 3: | |
prime_number = False | |
for factor in range(2, n): | |
if n % factor == 0: | |
prime_number = False | |
return prime_number | |
def find_next_prime(n): | |
start = n + 1 | |
found = False | |
the_next_prime = None | |
while not found: | |
if is_prime(start): | |
the_next_prime = start | |
found = True | |
else: | |
start += 1 | |
return the_next_prime | |
# test cases | |
# forgot how to use assertEqual | |
print("The next prime after 6 is: 7, {}".format(find_next_prime(6) == 7)) | |
print("The next prime after 10 is: 11, {}".format(find_next_prime(10) == 11)) | |
print("The next prime after 11 is: 13, {}".format(find_next_prime(11) == 13)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment