Created
January 12, 2015 18:54
-
-
Save sakamer71/188efc984ffb289a630e to your computer and use it in GitHub Desktop.
finding next prime - for rmotr
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): | |
| for i in range(2,n): | |
| if n%i == 0: | |
| return False | |
| return True | |
| def find_next_prime(n): | |
| mynum = n + 1 | |
| while not is_prime(mynum): | |
| mynum += 1 | |
| print "Next prime after {} is {}".format(n,mynum) | |
| return mynum | |
| print find_next_prime(input()) | |
| ## testing | |
| if __name__ == '__main__': | |
| assert find_next_prime(3) == 5, "next prime after 3" | |
| assert find_next_prime(7) == 11, "next prime after 7" | |
| assert find_next_prime(24) == 29, "next prime after 24" | |
| assert find_next_prime(151) == 157, "next prime after 151" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment