Skip to content

Improve sieve of eratosthenes and remove duplicate #5043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions maths/prime_sieve_eratosthenes.py

This file was deleted.

43 changes: 21 additions & 22 deletions maths/sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem
"""
from __future__ import annotations

import math


def prime_sieve(num: int) -> list[int]:
Expand All @@ -33,32 +30,34 @@ def prime_sieve(num: int) -> list[int]:
[]
"""

if num <= 0:
raise ValueError(f"{num}: Invalid input, please enter a positive integer.")
if num < 2:
return []

sieve = [True] * (num + 1)
prime = []
start = 2
end = int(math.sqrt(num))
sieve[0] = sieve[1] = False

while start <= end:
# If start is a prime
if sieve[start] is True:
prime.append(start)
# Do even numbers separately
primes = [2]
for i in range(4, num + 1, 2):
sieve[i] = False
Comment on lines +39 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want to optimize it like this to keep its original algorithm. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_and_variants

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the same algorithm, but since 2 is the only even prime, you can check it seperately and remove unnecessary tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest stick to Wiki.


# Set multiples of start be False
for i in range(start * start, num + 1, start):
if sieve[i] is True:
sieve[i] = False
p = 3
while p * p <= num:
Comment on lines +44 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use range here.

# If p is a prime
if sieve[p] is True:
primes.append(p)

start += 1
# Set multiples of start be False
for i in range(p * p, num + 1, p):
sieve[i] = False
p += 2

for j in range(end + 1, num + 1):
if sieve[j] is True:
prime.append(j)
for i in range(p, num + 1, 2):
if sieve[i] is True:
primes.append(i)

return prime
return primes


if __name__ == "__main__":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if __name__ == "__main__":
if __name__ == "__main__":
import doctest
doctest.testmod()

print(prime_sieve(int(input("Enter a positive integer: ").strip())))
print(prime_sieve(int(input("Get all primes less than or equal to: ").strip())))