-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Sieve of Eratosthenes optimization. #76
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
Conversation
Complexity and space reduced to a half.
The code gives wrong output for input- 0 and 1 |
math/primes_sieve_of_eratosthenes.py
Outdated
assert(x >= 0) | ||
# If x is even, exclude x from list (-1): | ||
sieve_size = (x//2 - 1) if x % 2 == 0 else (x//2) | ||
sieve = [1 for v in range(sieve_size)] # Sieve |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Atleast two spaces before inline comment.
math/primes_sieve_of_eratosthenes.py
Outdated
# If x is even, exclude x from list (-1): | ||
sieve_size = (x//2 - 1) if x % 2 == 0 else (x//2) | ||
sieve = [1 for v in range(sieve_size)] # Sieve | ||
primes = [2] # List of Primes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Atleast two spaces before inline comment.
math/primes_sieve_of_eratosthenes.py
Outdated
if sieve[i] == 1: | ||
value_at_i = i*2 + 3 | ||
primes.append(value_at_i) | ||
for j in range(i, sieve_size, value_at_i): | ||
sieve[j]=0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing white spaces around operator
Now return []
Fixed error for input, spaces before comment and operator. |
Complexity and space reduced to a half.