Sieve of Eratosthenes Understanding
I have this code I do not quite understand because I just started learning Python a week ago.
import numpy as np
import time
start_time=time.clock()
def Sieb(n): #Sieve
Eins = np.ones(n, dtype=bool) #Eins is just german for One
Eins[0] = Eins[1] = False #->I don't quite understand what
for i in range(2, n): #this one does.
if Eins[i]:
Eins[i*i::i] = False #Does this make the ones = zero?
return np.flatnonzero(Eins)
print Sieb(1000000)
print start_time
So, I understand the concept of the sieve (I guess) but I'm not sure how it is achieved here.
Where are the multiples of itself get to 0
and how does the np.flatnonzero
puts out the prime numbers because before that, they are just 1
and 0
?
I hope you can understand and help me. :)
python primes sieve
add a comment |
I have this code I do not quite understand because I just started learning Python a week ago.
import numpy as np
import time
start_time=time.clock()
def Sieb(n): #Sieve
Eins = np.ones(n, dtype=bool) #Eins is just german for One
Eins[0] = Eins[1] = False #->I don't quite understand what
for i in range(2, n): #this one does.
if Eins[i]:
Eins[i*i::i] = False #Does this make the ones = zero?
return np.flatnonzero(Eins)
print Sieb(1000000)
print start_time
So, I understand the concept of the sieve (I guess) but I'm not sure how it is achieved here.
Where are the multiples of itself get to 0
and how does the np.flatnonzero
puts out the prime numbers because before that, they are just 1
and 0
?
I hope you can understand and help me. :)
python primes sieve
1
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.
– 9769953
Nov 12 at 13:33
if Eins[i]: Eins[i*i::i] = False
: if ai
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.
– 9769953
Nov 12 at 13:34
1
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.
– 9769953
Nov 12 at 13:37
add a comment |
I have this code I do not quite understand because I just started learning Python a week ago.
import numpy as np
import time
start_time=time.clock()
def Sieb(n): #Sieve
Eins = np.ones(n, dtype=bool) #Eins is just german for One
Eins[0] = Eins[1] = False #->I don't quite understand what
for i in range(2, n): #this one does.
if Eins[i]:
Eins[i*i::i] = False #Does this make the ones = zero?
return np.flatnonzero(Eins)
print Sieb(1000000)
print start_time
So, I understand the concept of the sieve (I guess) but I'm not sure how it is achieved here.
Where are the multiples of itself get to 0
and how does the np.flatnonzero
puts out the prime numbers because before that, they are just 1
and 0
?
I hope you can understand and help me. :)
python primes sieve
I have this code I do not quite understand because I just started learning Python a week ago.
import numpy as np
import time
start_time=time.clock()
def Sieb(n): #Sieve
Eins = np.ones(n, dtype=bool) #Eins is just german for One
Eins[0] = Eins[1] = False #->I don't quite understand what
for i in range(2, n): #this one does.
if Eins[i]:
Eins[i*i::i] = False #Does this make the ones = zero?
return np.flatnonzero(Eins)
print Sieb(1000000)
print start_time
So, I understand the concept of the sieve (I guess) but I'm not sure how it is achieved here.
Where are the multiples of itself get to 0
and how does the np.flatnonzero
puts out the prime numbers because before that, they are just 1
and 0
?
I hope you can understand and help me. :)
python primes sieve
python primes sieve
edited Nov 12 at 13:37
tobias_k
56.7k967105
56.7k967105
asked Nov 12 at 13:30
Lennox
133
133
1
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.
– 9769953
Nov 12 at 13:33
if Eins[i]: Eins[i*i::i] = False
: if ai
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.
– 9769953
Nov 12 at 13:34
1
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.
– 9769953
Nov 12 at 13:37
add a comment |
1
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.
– 9769953
Nov 12 at 13:33
if Eins[i]: Eins[i*i::i] = False
: if ai
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.
– 9769953
Nov 12 at 13:34
1
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.
– 9769953
Nov 12 at 13:37
1
1
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.– 9769953
Nov 12 at 13:33
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.– 9769953
Nov 12 at 13:33
if Eins[i]: Eins[i*i::i] = False
: if a i
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.– 9769953
Nov 12 at 13:34
if Eins[i]: Eins[i*i::i] = False
: if a i
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.– 9769953
Nov 12 at 13:34
1
1
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.– 9769953
Nov 12 at 13:37
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.– 9769953
Nov 12 at 13:37
add a comment |
1 Answer
1
active
oldest
votes
Let's go through it step-by-step.
Eins = np.ones(n, dtype=bool)
This creates a new array of size n, with type bool
, and all ones. Because of the type, "one" means True
. The array represents all our numbers that we want to test for primality, with True
meaning the number is prime, False
meaning it is not. So we start with all numbers marked as (potential) primes.
Eins[0] = Eins[1] = False
Now we set the 0
th and 1
st element to False
: Neither 0 nor 1 are primes.
for i in range(2, n):
Next, we'll iterate over all remaining numbers (from 2 onwards). We could get away with only going up to the square root of n, but for simplicity, we go over all numbers.
if Eins[i]:
If the i
th value of the array is True
, that means i
is a prime. The first time this condition will be entered is with i=2
. Next, we want to remove all multiples of our number from the prime candidates:
Eins[i*i::i] = False
We can read this line as if it was Eins[2*i::i] = False
, starting from i*i
is just an optimization¹. If 2 is a prime number, that means that 2*2, 3*2, 4*2, ... aren't, so we set the multiples to False
. The indexing notation stands for "from i*i
until the end" (represented by the empty space between the colons) ", in steps of i
". This statement produces the numbers i*i
, i*(i+1)
, i*(i+2)
, ..., so all multiples of i
that haven't been marked as "not a prime" yet.
return np.flatnonzero(Eins)
And this simply returns all indices where the value is True
, i.e. all prime numbers that were found.
1: A word regarding the i*i
: We can start from the square of i
, because any numbers j*i
(for j < i
) have already been marked as nonprime when we were at j
.
Here's a demonstration that this works, for n=15
:
We start with the array filled with .ones
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]
Then we empty Eins[0]
and Eins[1]
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, T, T, T, T, T, T, T, T, T, T, T]
And now we start iterating over the range, starting with i=2
, and we remove every multiple of 2 starting with 2*2=4
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, T, F, T, F, T, F]
i=3
, removing every multiple of 3 starting with 3*3=9
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
Note that we didn't have to remove 6
, because it was already removed by i=2
.
When i=4
, we skip the removal because Eins[i]
is False
. From i=5
onwards, nothing happens, because the squares (25, 36, ...) are larger than the array. Finally, we use flatnonzero
and get all indices where the value is True
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
2 3 5 7 11 13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263249%2fsieve-of-eratosthenes-understanding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's go through it step-by-step.
Eins = np.ones(n, dtype=bool)
This creates a new array of size n, with type bool
, and all ones. Because of the type, "one" means True
. The array represents all our numbers that we want to test for primality, with True
meaning the number is prime, False
meaning it is not. So we start with all numbers marked as (potential) primes.
Eins[0] = Eins[1] = False
Now we set the 0
th and 1
st element to False
: Neither 0 nor 1 are primes.
for i in range(2, n):
Next, we'll iterate over all remaining numbers (from 2 onwards). We could get away with only going up to the square root of n, but for simplicity, we go over all numbers.
if Eins[i]:
If the i
th value of the array is True
, that means i
is a prime. The first time this condition will be entered is with i=2
. Next, we want to remove all multiples of our number from the prime candidates:
Eins[i*i::i] = False
We can read this line as if it was Eins[2*i::i] = False
, starting from i*i
is just an optimization¹. If 2 is a prime number, that means that 2*2, 3*2, 4*2, ... aren't, so we set the multiples to False
. The indexing notation stands for "from i*i
until the end" (represented by the empty space between the colons) ", in steps of i
". This statement produces the numbers i*i
, i*(i+1)
, i*(i+2)
, ..., so all multiples of i
that haven't been marked as "not a prime" yet.
return np.flatnonzero(Eins)
And this simply returns all indices where the value is True
, i.e. all prime numbers that were found.
1: A word regarding the i*i
: We can start from the square of i
, because any numbers j*i
(for j < i
) have already been marked as nonprime when we were at j
.
Here's a demonstration that this works, for n=15
:
We start with the array filled with .ones
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]
Then we empty Eins[0]
and Eins[1]
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, T, T, T, T, T, T, T, T, T, T, T]
And now we start iterating over the range, starting with i=2
, and we remove every multiple of 2 starting with 2*2=4
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, T, F, T, F, T, F]
i=3
, removing every multiple of 3 starting with 3*3=9
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
Note that we didn't have to remove 6
, because it was already removed by i=2
.
When i=4
, we skip the removal because Eins[i]
is False
. From i=5
onwards, nothing happens, because the squares (25, 36, ...) are larger than the array. Finally, we use flatnonzero
and get all indices where the value is True
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
2 3 5 7 11 13
add a comment |
Let's go through it step-by-step.
Eins = np.ones(n, dtype=bool)
This creates a new array of size n, with type bool
, and all ones. Because of the type, "one" means True
. The array represents all our numbers that we want to test for primality, with True
meaning the number is prime, False
meaning it is not. So we start with all numbers marked as (potential) primes.
Eins[0] = Eins[1] = False
Now we set the 0
th and 1
st element to False
: Neither 0 nor 1 are primes.
for i in range(2, n):
Next, we'll iterate over all remaining numbers (from 2 onwards). We could get away with only going up to the square root of n, but for simplicity, we go over all numbers.
if Eins[i]:
If the i
th value of the array is True
, that means i
is a prime. The first time this condition will be entered is with i=2
. Next, we want to remove all multiples of our number from the prime candidates:
Eins[i*i::i] = False
We can read this line as if it was Eins[2*i::i] = False
, starting from i*i
is just an optimization¹. If 2 is a prime number, that means that 2*2, 3*2, 4*2, ... aren't, so we set the multiples to False
. The indexing notation stands for "from i*i
until the end" (represented by the empty space between the colons) ", in steps of i
". This statement produces the numbers i*i
, i*(i+1)
, i*(i+2)
, ..., so all multiples of i
that haven't been marked as "not a prime" yet.
return np.flatnonzero(Eins)
And this simply returns all indices where the value is True
, i.e. all prime numbers that were found.
1: A word regarding the i*i
: We can start from the square of i
, because any numbers j*i
(for j < i
) have already been marked as nonprime when we were at j
.
Here's a demonstration that this works, for n=15
:
We start with the array filled with .ones
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]
Then we empty Eins[0]
and Eins[1]
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, T, T, T, T, T, T, T, T, T, T, T]
And now we start iterating over the range, starting with i=2
, and we remove every multiple of 2 starting with 2*2=4
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, T, F, T, F, T, F]
i=3
, removing every multiple of 3 starting with 3*3=9
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
Note that we didn't have to remove 6
, because it was already removed by i=2
.
When i=4
, we skip the removal because Eins[i]
is False
. From i=5
onwards, nothing happens, because the squares (25, 36, ...) are larger than the array. Finally, we use flatnonzero
and get all indices where the value is True
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
2 3 5 7 11 13
add a comment |
Let's go through it step-by-step.
Eins = np.ones(n, dtype=bool)
This creates a new array of size n, with type bool
, and all ones. Because of the type, "one" means True
. The array represents all our numbers that we want to test for primality, with True
meaning the number is prime, False
meaning it is not. So we start with all numbers marked as (potential) primes.
Eins[0] = Eins[1] = False
Now we set the 0
th and 1
st element to False
: Neither 0 nor 1 are primes.
for i in range(2, n):
Next, we'll iterate over all remaining numbers (from 2 onwards). We could get away with only going up to the square root of n, but for simplicity, we go over all numbers.
if Eins[i]:
If the i
th value of the array is True
, that means i
is a prime. The first time this condition will be entered is with i=2
. Next, we want to remove all multiples of our number from the prime candidates:
Eins[i*i::i] = False
We can read this line as if it was Eins[2*i::i] = False
, starting from i*i
is just an optimization¹. If 2 is a prime number, that means that 2*2, 3*2, 4*2, ... aren't, so we set the multiples to False
. The indexing notation stands for "from i*i
until the end" (represented by the empty space between the colons) ", in steps of i
". This statement produces the numbers i*i
, i*(i+1)
, i*(i+2)
, ..., so all multiples of i
that haven't been marked as "not a prime" yet.
return np.flatnonzero(Eins)
And this simply returns all indices where the value is True
, i.e. all prime numbers that were found.
1: A word regarding the i*i
: We can start from the square of i
, because any numbers j*i
(for j < i
) have already been marked as nonprime when we were at j
.
Here's a demonstration that this works, for n=15
:
We start with the array filled with .ones
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]
Then we empty Eins[0]
and Eins[1]
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, T, T, T, T, T, T, T, T, T, T, T]
And now we start iterating over the range, starting with i=2
, and we remove every multiple of 2 starting with 2*2=4
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, T, F, T, F, T, F]
i=3
, removing every multiple of 3 starting with 3*3=9
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
Note that we didn't have to remove 6
, because it was already removed by i=2
.
When i=4
, we skip the removal because Eins[i]
is False
. From i=5
onwards, nothing happens, because the squares (25, 36, ...) are larger than the array. Finally, we use flatnonzero
and get all indices where the value is True
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
2 3 5 7 11 13
Let's go through it step-by-step.
Eins = np.ones(n, dtype=bool)
This creates a new array of size n, with type bool
, and all ones. Because of the type, "one" means True
. The array represents all our numbers that we want to test for primality, with True
meaning the number is prime, False
meaning it is not. So we start with all numbers marked as (potential) primes.
Eins[0] = Eins[1] = False
Now we set the 0
th and 1
st element to False
: Neither 0 nor 1 are primes.
for i in range(2, n):
Next, we'll iterate over all remaining numbers (from 2 onwards). We could get away with only going up to the square root of n, but for simplicity, we go over all numbers.
if Eins[i]:
If the i
th value of the array is True
, that means i
is a prime. The first time this condition will be entered is with i=2
. Next, we want to remove all multiples of our number from the prime candidates:
Eins[i*i::i] = False
We can read this line as if it was Eins[2*i::i] = False
, starting from i*i
is just an optimization¹. If 2 is a prime number, that means that 2*2, 3*2, 4*2, ... aren't, so we set the multiples to False
. The indexing notation stands for "from i*i
until the end" (represented by the empty space between the colons) ", in steps of i
". This statement produces the numbers i*i
, i*(i+1)
, i*(i+2)
, ..., so all multiples of i
that haven't been marked as "not a prime" yet.
return np.flatnonzero(Eins)
And this simply returns all indices where the value is True
, i.e. all prime numbers that were found.
1: A word regarding the i*i
: We can start from the square of i
, because any numbers j*i
(for j < i
) have already been marked as nonprime when we were at j
.
Here's a demonstration that this works, for n=15
:
We start with the array filled with .ones
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]
Then we empty Eins[0]
and Eins[1]
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, T, T, T, T, T, T, T, T, T, T, T]
And now we start iterating over the range, starting with i=2
, and we remove every multiple of 2 starting with 2*2=4
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, T, F, T, F, T, F]
i=3
, removing every multiple of 3 starting with 3*3=9
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
Note that we didn't have to remove 6
, because it was already removed by i=2
.
When i=4
, we skip the removal because Eins[i]
is False
. From i=5
onwards, nothing happens, because the squares (25, 36, ...) are larger than the array. Finally, we use flatnonzero
and get all indices where the value is True
:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[F, F, T, T, F, T, F, T, F, F, F, T, F, T, F]
2 3 5 7 11 13
edited Nov 12 at 14:10
answered Nov 12 at 13:59
L3viathan
15.4k12847
15.4k12847
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263249%2fsieve-of-eratosthenes-understanding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Eins[0] = Eins[1] = False
: because 0 and 1 are not primes.– 9769953
Nov 12 at 13:33
if Eins[i]: Eins[i*i::i] = False
: if ai
number is False (= not a prime), set its square and every i-th following number to False/not a prime. That is the sieve part.– 9769953
Nov 12 at 13:34
1
Note that the sieving part starts at the square, since every variant below that (2*i, 3*i, ...) has already been filtered out in a previous step (namely, for primes 2, 3, 5, ...).
– 9769953
Nov 12 at 13:35
Then looping from 2 to n, if Eins[i] is non-zero (i.e. a prime), we start from its square and set every ith element (i.e. its multiples) to False (i.e. Non-prime).
– Nitin Pawar
Nov 12 at 13:36
flatnontzero
is properly documented: it returns the (flattened) indices of non-zero (non-False, non-prime) numbers. Since we're using the indices as actual numbers to run the sieve on, that gives the primes.– 9769953
Nov 12 at 13:37