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/math/prime_numbers.py b/allalgorithms/math/prime_numbers.py new file mode 100644 index 0000000..b16641a --- /dev/null +++ b/allalgorithms/math/prime_numbers.py @@ -0,0 +1,31 @@ +# -*- 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): + if n < 1 or n > SIEVE_LIMIT: + return [] + prime = sieve(n) + ans = [] + for i in range(n+1): + if prime[i]: + ans.append(i) + return ans diff --git a/allalgorithms/numeric/__init__.py b/allalgorithms/numeric/__init__.py deleted file mode 100644 index cd1f5cc..0000000 --- a/allalgorithms/numeric/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .max_numbers import find_max \ No newline at end of file diff --git a/docs/math/prime-numbers.md b/docs/math/prime-numbers.md new file mode 100644 index 0000000..5c67fc7 --- /dev/null +++ b/docs/math/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.math 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_math.py similarity index 82% rename from tests/test_numeric.py rename to tests/test_math.py index 2be532d..46b2353 100644 --- a/tests/test_numeric.py +++ b/tests/test_math.py @@ -1,7 +1,8 @@ import unittest -from allalgorithms.numeric import find_max - +from allalgorithms.math import ( + find_max +) class TestMax(unittest.TestCase):