Skip to content

Commit 938dd0b

Browse files
nikolasvargascclauss
authored andcommitted
improved prime numbers implementation (#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

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

DIRECTORY.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
* [Gaussian Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/gaussian_filter.py)
134134
* [Median Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/median_filter.py)
135135
* [Sobel Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/sobel_filter.py)
136+
* [Index Calculation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/index_calculation.py)
136137
* Rotation
137138
* [Rotation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/rotation/rotation.py)
138139
* [Test Digital Image Processing](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/test_digital_image_processing.py)
@@ -216,6 +217,7 @@
216217
## Hashes
217218
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
218219
* [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py)
220+
* [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py)
219221
* [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py)
220222
* [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py)
221223

@@ -234,6 +236,7 @@
234236
* [Linear Discriminant Analysis](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_discriminant_analysis.py)
235237
* [Linear Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_regression.py)
236238
* [Logistic Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/logistic_regression.py)
239+
* [Multilayer Perceptron Classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/multilayer_perceptron_classifier.py)
237240
* [Polymonial Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/polymonial_regression.py)
238241
* [Scoring Functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
239242
* [Sequential Minimum Optimization](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/sequential_minimum_optimization.py)
@@ -252,6 +255,7 @@
252255
* [Binomial Coefficient](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_coefficient.py)
253256
* [Ceil](https://github.com/TheAlgorithms/Python/blob/master/maths/ceil.py)
254257
* [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py)
258+
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
255259
* [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py)
256260
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
257261
* [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py)
@@ -286,6 +290,7 @@
286290
* [Prime Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py)
287291
* [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_numbers.py)
288292
* [Prime Sieve Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_sieve_eratosthenes.py)
293+
* [Pythagoras](https://github.com/TheAlgorithms/Python/blob/master/maths/pythagoras.py)
289294
* [Qr Decomposition](https://github.com/TheAlgorithms/Python/blob/master/maths/qr_decomposition.py)
290295
* [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/quadratic_equations_complex_numbers.py)
291296
* [Radix2 Fft](https://github.com/TheAlgorithms/Python/blob/master/maths/radix2_fft.py)
@@ -499,11 +504,11 @@
499504
* [I Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/i_sort.py)
500505
* [Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/insertion_sort.py)
501506
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
502-
* [Merge Sort Fastest](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort_fastest.py)
503507
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
504508
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
505509
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)
506510
* [Pigeon Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeon_sort.py)
511+
* [Pigeonhole Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeonhole_sort.py)
507512
* [Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py)
508513
* [Quick Sort 3 Partition](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort_3_partition.py)
509514
* [Radix Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/radix_sort.py)
@@ -516,6 +521,7 @@
516521
* [Tim Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tim_sort.py)
517522
* [Topological Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/topological_sort.py)
518523
* [Tree Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tree_sort.py)
524+
* [Unknown Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/unknown_sort.py)
519525
* [Wiggle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/wiggle_sort.py)
520526

521527
## Strings

data_structures/data_structures/heap/heap_generic.py

+2-1
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

+4-4
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

+25-19
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def pigeonhole_sort(a):
3636

3737

3838
def main():
39-
pigeonhole_sort([8, 3, 2, 7, 4, 6, 8])
40-
print("Sorted order is: ", " ", join(a))
39+
a = [8, 3, 2, 7, 4, 6, 8]
40+
pigeonhole_sort(a)
41+
print("Sorted order is:", " ".join(a))
4142

4243

4344
if __name__ == "__main__":

0 commit comments

Comments
 (0)