Skip to content

Instantly share code, notes, and snippets.

@Zaargh
Created June 17, 2016 21:45
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 Zaargh/625af3cb1f3a4f8bf2f75ce86cab20a1 to your computer and use it in GitHub Desktop.
Save Zaargh/625af3cb1f3a4f8bf2f75ce86cab20a1 to your computer and use it in GitHub Desktop.
# primes.py
from math import sqrt
def find_next_prime(n):
n = int(n) # Allows for floating point inputs
if n < 2:
return 2 # Primes are natural numbers, 2 is the smallest possible
while True:
n += 1
for i in range(2, int(sqrt(n))):
if n % i == 0:
break
else:
return n
###############################################################
# primes_test.py
#!/usr/bin/env python3
import unittest
from primes import find_next_prime
class PrimesTest(unittest.TestCase):
def test_next_6(self):
self.assertEqual(find_next_prime(6), 7)
def test_next_after_prime(self):
self.assertEqual(find_next_prime(11), 13)
def test_negative_input(self):
self.assertEqual(find_next_prime(-1234), 2)
def test_float_input(self):
self.assertEqual(find_next_prime(6.5), 7)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment