improved prime numbers implementation (#1606) · zinating/algorithms-python@938dd0b · GitHub
Skip to content

Commit 938dd0b

Browse files
nikolasvargascclauss
authored andcommitted
improved prime numbers implementation (TheAlgorithms#1606)
* improved prime numbers implementation * fixup! Format Python code with psf/black push * fix type hint * fixup! Format Python code with psf/black push * fix doctests * updating DIRECTORY.md * added prime tests with negative numbers * using for instead filter * updating DIRECTORY.md * Remove unused typing.List * Remove tab indentation * print("Sorted order is:", " ".join(a))
1 parent ccc1ff2 commit 938dd0b

5 files changed

Lines changed: 41 additions & 27 deletions

File tree

DIRECTORY.md

Lines changed: 7 additions & 1 deletion

data_structures/data_structures/heap/heap_generic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def _swap(self, i, j):
3030
"""Performs changes required for swapping two elements in the heap"""
3131
# First update the indexes of the items in index map.
3232
self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = (
33-
self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]]
33+
self.pos_map[self.arr[j][0]],
34+
self.pos_map[self.arr[i][0]],
3435
)
3536
# Then swap the items in the list.
3637
self.arr[i], self.arr[j] = self.arr[j], self.arr[i]

digital_image_processing/index_calculation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def calculation(
176176

177177
def ARVI2(self):
178178
"""
179-
Atmospherically Resistant Vegetation Index 2
180-
https://www.indexdatabase.de/db/i-single.php?id=396
181-
:return: index
182-
−0.18+1.17*(self.nir−self.red)/(self.nir+self.red)
179+
Atmospherically Resistant Vegetation Index 2
180+
https://www.indexdatabase.de/db/i-single.php?id=396
181+
:return: index
182+
−0.18+1.17*(self.nir−self.red)/(self.nir+self.red)
183183
"""
184184
return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
185185

maths/prime_numbers.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
from typing import List
1+
from typing import Generator
22

33

4-
def primes(max: int) -> List[int]:
4+
def primes(max: int) -> Generator[int, None, None]:
55
"""
66
Return a list of all primes numbers up to max.
7-
>>> primes(10)
8-
[2, 3, 5, 7]
9-
>>> primes(11)
10-
[2, 3, 5, 7, 11]
11-
>>> primes(25)
7+
>>> list(primes(0))
8+
[]
9+
>>> list(primes(-1))
10+
[]
11+
>>> list(primes(-10))
12+
[]
13+
>>> list(primes(25))
1214
[2, 3, 5, 7, 11, 13, 17, 19, 23]
13-
>>> primes(1_000_000)[-1]
14-
999983
15+
>>> list(primes(11))
16+
[2, 3, 5, 7, 11]
17+
>>> list(primes(33))
18+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
19+
>>> list(primes(10000))[-1]
20+
9973
1521
"""
16-
max += 1
17-
numbers = [False] * max
18-
ret = []
19-
for i in range(2, max):
20-
if not numbers[i]:
21-
for j in range(i, max, i):
22-
numbers[j] = True
23-
ret.append(i)
24-
return ret
22+
numbers: Generator = (i for i in range(1, (max + 1)))
23+
for i in (n for n in numbers if n > 1):
24+
for j in range(2, i):
25+
if (i % j) == 0:
26+
break
27+
else:
28+
yield i
2529

2630

2731
if __name__ == "__main__":
28-
print(primes(int(input("Calculate primes up to:\n>> "))))
32+
number = int(input("Calculate primes up to:\n>> ").strip())
33+
for ret in primes(number):
34+
print(ret)

sorts/pigeonhole_sort.py

Lines changed: 3 additions & 2 deletions

0 commit comments

Comments
 (0)