13
1
The Challenge
Implement the Sundaram sieve for finding prime numbers below n
. Take an input integer, n
, and output the prime numbers below n
. You can assume that n
will always be less than or equal to one million.
Sieve
Start with a list of the integers from
1
ton
.Remove all numbers that are in the form
i + j + 2ij
where:i
andj
are less thann
.j
is always greater than or equal toi
, which is greater than or equal to1
.i + j + 2ij
is less than or equal ton
Multiply the remaining numbers by
2
, and add1
.
This will yield all the prime numbers (except 2
, which should be included in your output) less than 2n + 2
.
Here is an animation of the sieve being used to find primes below 202
.
Output
Your output should be every prime integer ≤ n
(in ascending order) followed by a newline:
2
3
5
Where n
is 5
.
Examples
> 10
2
3
5
7
> 30
2
3
5
7
11
13
17
19
23
29
Inputs are denoted by >
.
Your example with
n=30
is missing 29 in the output. – isaacg – 2015-09-30T18:57:34.8735A trouble with challenges that ask to use a specific method is that it's not clear what modifications one can make. For example, your description checks only
(i,j)
withi<=j
, but the result doesn't change if we ignore this requirement. Can we do so to save bytes? – xnor – 2015-09-30T19:25:23.233I never said that you had to check if
i <= j
. It's just part of how the sieve works. So yes, you can leave out thei <= j
in your code. @xnor – Zach Gates – 2015-09-30T19:37:54.2202How much leeway do we have here? The sieve is equivalent to selecting all odd numbers (because the results are of the form
2n+1
) which are not of the form2(i + j + 2ij)+1
- can we test this property directly on the potential primes or does our code have to do the times 2 plus 1 at some point? – Martin Ender – 2015-09-30T20:22:04.977You must multiply the filtered results by two and add one. @MartinBüttner – Zach Gates – 2015-09-30T20:27:59.837
Nice challenge! I haven't heard of this method before. I got confused at "except two", as I thought it meant there were two primes not included. Perhaps you should just change it to "except
2
" or something similar. Also, I believe you forgot the5
in the first example (wheren
is5
), unless this was intentional. – ETHproductions – 2015-09-30T21:07:29.080Thanks for the suggestion! I've also fixed the example. @ETHproductions – Zach Gates – 2015-09-30T21:36:50.173
i
must be >1,j
must be >i, so the sieve starts with i=2,j=3 and the first eliminated number isi + j + 2ij -> 2 + 3 + 2*2*3 = 17
. How is the example eliminating 4,7,10,12,13,15...? – TessellatingHeckler – 2015-10-01T02:20:02.2501I'm a little confused by what
n
is in the whole thing. In the method description, it says that it will generate all primes up to2 * n + 2
. But in the input/output description, it says that the input isn
, and the output all primes up ton
. So are we supposed to apply the method to generate all primes up to2 * n + 2
, and then drop the ones larger thann
for the output? Or should we calculate then
in the method description from the inputn
? – Reto Koradi – 2015-10-01T03:56:01.300You should calculate and then drop the ones larger than
n
. @RetoKoradi – Zach Gates – 2015-10-01T04:00:17.513There's a difference between text and formula. In the text, it says "j is always greater than i, which is greater than 1". In the formula, it shows
>=
for both of these, not>
. – Reto Koradi – 2015-10-01T05:23:44.060I've fixed it. @RetoKoradi – Zach Gates – 2015-10-01T05:26:58.583
I assume it is legit to take n >= 2 as granted (considering it doesnt make sense to look for primes smaller than 2) as all of the solutions dont consider that case? – enpenax – 2015-10-01T11:09:03.743
Does the initial array have to start with 1, or can it start with 0 to simplify indexing? Do all of the calculations have to be done in that order, or can you pre-calculate the
2n + 2
values? – Brad Gilbert b2gills – 2015-11-30T00:31:39.273