Skip to content

Instantly share code, notes, and snippets.

@PlugaruT
Created April 28, 2016 10:42
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 PlugaruT/2209b500a03eb9aa2c58c2d9e080186a to your computer and use it in GitHub Desktop.
Save PlugaruT/2209b500a03eb9aa2c58c2d9e080186a to your computer and use it in GitHub Desktop.
### 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.
#
### End Assignment ###
import unittest
def find_prime_in_range(min, max):
for number in range(min, max):
for i in range(2, number):
if number % i == 0:
break
else:
return number
return None
def find_next_prime(n):
return find_prime_in_range(n + 1, 2 * (n + 1))
class FindNextPrimeTestCase(unittest.TestCase):
def test_number_6(self):
self.assertEqual(find_next_prime(6), 7)
def test_number_10(self):
self.assertEqual(find_next_prime(10), 11)
def test_number_11(self):
self.assertEqual(find_next_prime(11), 13)
if __name__ == '__main__':
print find_next_prime(6)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment