Fix sieve
This commit is contained in:
parent
a24f670f16
commit
419ba8d44d
@ -73,7 +73,7 @@ def lcmm(values, n):
|
|||||||
|
|
||||||
# Function implementing the Sieve or Eratosthenes to generate primes up to a certain number.
|
# Function implementing the Sieve or Eratosthenes to generate primes up to a certain number.
|
||||||
def sieve(n):
|
def sieve(n):
|
||||||
primes = [1] * n
|
primes = [1] * (n + 1)
|
||||||
|
|
||||||
# 0 and 1 are not prime, 2 and 3 are prime.
|
# 0 and 1 are not prime, 2 and 3 are prime.
|
||||||
primes[0] = 0
|
primes[0] = 0
|
||||||
@ -86,7 +86,7 @@ def sieve(n):
|
|||||||
# If i is prime, all multiples of i smaller than i*i have already been crossed out.
|
# If i is prime, all multiples of i smaller than i*i have already been crossed out.
|
||||||
# if i=sqrt(n), all multiples of i up to n (the target) have been crossed out. So
|
# if i=sqrt(n), all multiples of i up to n (the target) have been crossed out. So
|
||||||
# there is no need check i>sqrt(n).
|
# there is no need check i>sqrt(n).
|
||||||
limit = floor(sqrt(n))
|
limit = ceil(sqrt(n + 1))
|
||||||
|
|
||||||
for i in range(3, limit, 2):
|
for i in range(3, limit, 2):
|
||||||
# Find the next number not crossed out, which is prime.
|
# Find the next number not crossed out, which is prime.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user