Skip to content

Commit b6d4bb0

Browse files
author
Rodolfo Quispe
committed
Added docs, test and format for code
1 parent dc4137a commit b6d4bb0

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

allalgorithms/numeric/prime_numbers.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ def sieve(n):
2121
return prime
2222

2323
def prime_lower(n):
24-
"""
25-
inputs:
26-
n: integer
27-
28-
outputs: list of prime numbers less or equal than n
29-
"""
30-
assert n>=1 and n<=SIEVE_LIMIT, "input value is not valid"
24+
if n < 1 or n > SIEVE_LIMIT:
25+
return []
3126
prime = sieve(n)
3227
ans = []
3328
for i in range(n+1):

docs/numeric/prime-numbers.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Prime Numbers
2+
3+
Using prime numbers is common in many problems. We implement various algorithms using them.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.numeric import prime_lower
15+
16+
print(prime_lower(18))
17+
# -> [2, 3, 5, 7, 11, 13, 17]
18+
19+
print(prime_lower(-1))
20+
# -> []
21+
22+
print(prime_lower(1000001))
23+
# -> []
24+
```
25+
26+
## API
27+
28+
### prime_lower(query)
29+
30+
> Return array of prime numbers lower than `query`, `query` must be lower than `10e6` to avoid memory problems.
31+
32+
##### Params:
33+
34+
- `query`: superior limit for the array of prime number

tests/test_numeric.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import unittest
22

3-
from allalgorithms.numeric import find_max
4-
3+
from allalgorithms.numeric import (
4+
find_max,
5+
prime_lower
6+
)
57

68
class TestMax(unittest.TestCase):
79

810
def test_find_max_value(self):
911
test_list = [3, 1, 8, 7, 4]
1012
self.assertEqual(8, find_max(test_list))
1113

14+
class TestPrimeLower(unittest.TestCase):
15+
16+
def test_prime_lower_value(self):
17+
test_list =[2, 3, 5, 7, 11, 13, 17]
18+
self.assertEqual(prime_lower(18), test_list)
19+
20+
1221
if __name__ == "__main__":
1322
unittest.main()

0 commit comments

Comments
 (0)