From dc4137abcd44ad0d0afaf0589b73d835be60c828 Mon Sep 17 00:00:00 2001 From: Rodolfo Quispe Date: Wed, 2 Oct 2019 00:12:26 -0500 Subject: [PATCH 1/6] Added algorithm to compute prime numbers less than x --- allalgorithms/numeric/__init__.py | 3 ++- allalgorithms/numeric/prime_numbers.py | 36 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 allalgorithms/numeric/prime_numbers.py diff --git a/allalgorithms/numeric/__init__.py b/allalgorithms/numeric/__init__.py index cd1f5cc..4704ef7 100644 --- a/allalgorithms/numeric/__init__.py +++ b/allalgorithms/numeric/__init__.py @@ -1 +1,2 @@ -from .max_numbers import find_max \ No newline at end of file +from .max_numbers import find_max +from .prime_numbers import prime_lower diff --git a/allalgorithms/numeric/prime_numbers.py b/allalgorithms/numeric/prime_numbers.py new file mode 100644 index 0000000..73b383a --- /dev/null +++ b/allalgorithms/numeric/prime_numbers.py @@ -0,0 +1,36 @@ +# -*- coding: UTF-8 -*- +# +# Numeric Algorithms +# The All â–²lgorithms library for python +# +# Contributed by: Rodolfo +# Github: @RQuispeC +# +import math + +SIEVE_LIMIT = 1000000 + +def sieve(n): + prime = [True]*(n+1) + prime[0] = False + prime[1] = False + for i in range(2,int(math.sqrt(n)) + 1): + if prime[i]: + for j in range(i*i, n+1, i): + prime[j] = False + return prime + +def prime_lower(n): + """ + inputs: + n: integer + + outputs: list of prime numbers less or equal than n + """ + assert n>=1 and n<=SIEVE_LIMIT, "input value is not valid" + prime = sieve(n) + ans = [] + for i in range(n+1): + if prime[i]: + ans.append(i) + return ans From b6d4bb0e5900d6056d3d5f90b18f5b641842269b Mon Sep 17 00:00:00 2001 From: Rodolfo Quispe Date: Wed, 2 Oct 2019 12:20:01 -0500 Subject: [PATCH 2/6] Added docs, test and format for code --- allalgorithms/numeric/prime_numbers.py | 9 ++----- docs/numeric/prime-numbers.md | 34 ++++++++++++++++++++++++++ tests/test_numeric.py | 13 ++++++++-- 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 docs/numeric/prime-numbers.md diff --git a/allalgorithms/numeric/prime_numbers.py b/allalgorithms/numeric/prime_numbers.py index 73b383a..b16641a 100644 --- a/allalgorithms/numeric/prime_numbers.py +++ b/allalgorithms/numeric/prime_numbers.py @@ -21,13 +21,8 @@ def sieve(n): return prime def prime_lower(n): - """ - inputs: - n: integer - - outputs: list of prime numbers less or equal than n - """ - assert n>=1 and n<=SIEVE_LIMIT, "input value is not valid" + if n < 1 or n > SIEVE_LIMIT: + return [] prime = sieve(n) ans = [] for i in range(n+1): diff --git a/docs/numeric/prime-numbers.md b/docs/numeric/prime-numbers.md new file mode 100644 index 0000000..010c901 --- /dev/null +++ b/docs/numeric/prime-numbers.md @@ -0,0 +1,34 @@ +# Prime Numbers + +Using prime numbers is common in many problems. We implement various algorithms using them. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.numeric import prime_lower + +print(prime_lower(18)) +# -> [2, 3, 5, 7, 11, 13, 17] + +print(prime_lower(-1)) +# -> [] + +print(prime_lower(1000001)) +# -> [] +``` + +## API + +### prime_lower(query) + +> Return array of prime numbers lower than `query`, `query` must be lower than `10e6` to avoid memory problems. + +##### Params: + +- `query`: superior limit for the array of prime number diff --git a/tests/test_numeric.py b/tests/test_numeric.py index 2be532d..7a9f6d7 100644 --- a/tests/test_numeric.py +++ b/tests/test_numeric.py @@ -1,7 +1,9 @@ import unittest -from allalgorithms.numeric import find_max - +from allalgorithms.numeric import ( + find_max, + prime_lower +) class TestMax(unittest.TestCase): @@ -9,5 +11,12 @@ def test_find_max_value(self): test_list = [3, 1, 8, 7, 4] self.assertEqual(8, find_max(test_list)) +class TestPrimeLower(unittest.TestCase): + + def test_prime_lower_value(self): + test_list =[2, 3, 5, 7, 11, 13, 17] + self.assertEqual(prime_lower(18), test_list) + + if __name__ == "__main__": unittest.main() From c95441fabb3ddc9681e7aabb2bd7a23746868ae2 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 2 Oct 2019 14:44:09 -0400 Subject: [PATCH 3/6] Renaming numeric to math --- docs/{numeric => math}/prime-numbers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename docs/{numeric => math}/prime-numbers.md (91%) diff --git a/docs/numeric/prime-numbers.md b/docs/math/prime-numbers.md similarity index 91% rename from docs/numeric/prime-numbers.md rename to docs/math/prime-numbers.md index 010c901..5c67fc7 100644 --- a/docs/numeric/prime-numbers.md +++ b/docs/math/prime-numbers.md @@ -11,7 +11,7 @@ pip install allalgorithms ## Usage ```py -from allalgorithms.numeric import prime_lower +from allalgorithms.math import prime_lower print(prime_lower(18)) # -> [2, 3, 5, 7, 11, 13, 17] From a7b2a25d76e764f4a735b3024a4873dee52a9d14 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 2 Oct 2019 14:44:42 -0400 Subject: [PATCH 4/6] Update and rename allalgorithms/numeric/prime_numbers.py to allalgorithms/math/prime_numbers.py --- allalgorithms/{numeric => math}/prime_numbers.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename allalgorithms/{numeric => math}/prime_numbers.py (100%) diff --git a/allalgorithms/numeric/prime_numbers.py b/allalgorithms/math/prime_numbers.py similarity index 100% rename from allalgorithms/numeric/prime_numbers.py rename to allalgorithms/math/prime_numbers.py From 4d63f53a155d0a628de88f47da01a131c530ddfd Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 2 Oct 2019 14:46:03 -0400 Subject: [PATCH 5/6] Update and rename test_numeric.py to test_math.py --- tests/test_math.py | 14 ++++++++++++++ tests/test_numeric.py | 22 ---------------------- 2 files changed, 14 insertions(+), 22 deletions(-) create mode 100644 tests/test_math.py delete mode 100644 tests/test_numeric.py diff --git a/tests/test_math.py b/tests/test_math.py new file mode 100644 index 0000000..46b2353 --- /dev/null +++ b/tests/test_math.py @@ -0,0 +1,14 @@ +import unittest + +from allalgorithms.math import ( + find_max +) + +class TestMax(unittest.TestCase): + + def test_find_max_value(self): + test_list = [3, 1, 8, 7, 4] + self.assertEqual(8, find_max(test_list)) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_numeric.py b/tests/test_numeric.py deleted file mode 100644 index 7a9f6d7..0000000 --- a/tests/test_numeric.py +++ /dev/null @@ -1,22 +0,0 @@ -import unittest - -from allalgorithms.numeric import ( - find_max, - prime_lower -) - -class TestMax(unittest.TestCase): - - def test_find_max_value(self): - test_list = [3, 1, 8, 7, 4] - self.assertEqual(8, find_max(test_list)) - -class TestPrimeLower(unittest.TestCase): - - def test_prime_lower_value(self): - test_list =[2, 3, 5, 7, 11, 13, 17] - self.assertEqual(prime_lower(18), test_list) - - -if __name__ == "__main__": - unittest.main() From 6be290fc79e0e6a421b39f83ef9f908b6a424acb Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 2 Oct 2019 14:47:13 -0400 Subject: [PATCH 6/6] Update and rename allalgorithms/numeric/__init__.py to allalgorithms/math/__init__.py --- allalgorithms/math/__init__.py | 1 + allalgorithms/numeric/__init__.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) create mode 100644 allalgorithms/math/__init__.py delete mode 100644 allalgorithms/numeric/__init__.py diff --git a/allalgorithms/math/__init__.py b/allalgorithms/math/__init__.py new file mode 100644 index 0000000..3ada627 --- /dev/null +++ b/allalgorithms/math/__init__.py @@ -0,0 +1 @@ +from .max_numbers import find_max diff --git a/allalgorithms/numeric/__init__.py b/allalgorithms/numeric/__init__.py deleted file mode 100644 index 4704ef7..0000000 --- a/allalgorithms/numeric/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .max_numbers import find_max -from .prime_numbers import prime_lower