Skip to content

Commit

Permalink
#20 Add real sieve algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sylhare committed May 11, 2020
1 parent b25c2fc commit f16aec8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
18 changes: 18 additions & 0 deletions nprime/pyprime.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ def sieve_eratosthenes(upper):
"""
Implementation of the sieve of erathostenes that discover the primes and their composite up to a limit.
:return: a dictionary,
the key are the primes up to n
the value is the list of composites of these primes up to n
"""
primes = {}
composites = set()
for p in range(2, upper + 1):
if p not in composites:
primes[p] = range(p * p, upper + 1, p)
composites.update(primes[p])

return primes


def trial_division(upper):
"""
Implementation of the trial division that discover the primes and all their divider up to a limit.
:return: a dictionary,
the key are the primes up to n
the value is the list of composites of these primes up to n
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
LONG_DESCRIPTION = convert("README.md")

setup(name='nprime',
version='1.0.0',
version='1.1.0',
description='Python library for primes algorithms',
long_description=LONG_DESCRIPTION,
author='sylhare',
Expand Down
11 changes: 6 additions & 5 deletions tests/test_eratosthenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ class TestEratosthenes(unittest.TestCase):

def test_001_is_not_integer_return_error(self):
""" there should only be 2 in the list, if limit is set to 2 """
self.assertRaises(ValueError, sieve, 3.8)
self.assertRaises(TypeError, sieve, 3.8)

def test_002_is_inferior_to_two_return_error(self):
""" there should only be 2 in the list, if limit is set to 2 """
self.assertRaises(ValueError, sieve, -1)
self.assertEquals({}, sieve(-1))

def test_003_is_sieve_showing_primes_and_composite(self):
""" The sieve should work for the first 10 numbers"""
self.assertTrue(sieve(10) == {2: [4, 6, 8], 3: [6, 9], 5: [], 7: []})
self.assertEquals({2: [4, 6, 8, 10], 3: [9], 5: [], 7: []}, sieve(10))

@unittest.skip("Does not work with Travis CI")
def test_004_are_all_keys_primes(self):
""" Making sure that all keys from the sieve are prime """
self.assertTrue(list(sieve(70)) == FIRST_PRIMES)
primes = list(sieve(70))
primes.sort()
self.assertEqual(FIRST_PRIMES, primes)


if __name__ == '__main__':
Expand Down
30 changes: 30 additions & 0 deletions tests/test_trial_division.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from nprime.pyprime import trial_division as trial_division
from tests.prime_testcase import FIRST_PRIMES


class TestTrialDivision(unittest.TestCase):
""" Tests for generate prime function """

def test_001_is_not_integer_return_error(self):
""" there should only be 2 in the list, if limit is set to 2 """
self.assertRaises(ValueError, trial_division, 3.8)

def test_002_is_inferior_to_two_return_error(self):
""" there should only be 2 in the list, if limit is set to 2 """
self.assertRaises(ValueError, trial_division, -1)

def test_003_is_trial_division_showing_primes_and_composite(self):
""" The trial_division should work for the first 10 numbers"""
self.assertEquals({2: [4, 6, 8], 3: [6, 9], 5: [], 7: []}, trial_division(10))

def test_004_are_all_keys_primes(self):
""" Making sure that all keys from the trial_division are prime """
primes = list(trial_division(70).keys())
primes.sort()
self.assertEqual(FIRST_PRIMES, primes)


if __name__ == '__main__':
unittest.main()

0 comments on commit f16aec8

Please sign in to comment.