4

I was asked this question in an interview. I implemented an algorithm using sieve of eratosthenes concept and an array.

Is there a better way to go about this question For those who dont know the sieve , here is the link:

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

EDIT: Best in terms of both time and space complexity. I just told them the flaw of SoE is space complexity. So they asked me if I could do something about it . Here is how the interview went about: 1) Implement a algo that prints prime numbers from 1 to n Ans: I implement using SoE 2) Is this the best way to go about it Ans: ???

starblue
  • 51,675
  • 14
  • 88
  • 146
tomkaith13
  • 1,625
  • 3
  • 26
  • 36
  • I'd likely walk out of the interview if asked that question. Puzzle questions show they are looking for people with intelligence. Questions like that just mean the interviewer is being arrogant. – riwalk Mar 16 '11 at 17:28
  • Wait...did they ask you if there was something better than the Sieve of Eratosthenes, or did they just ask for prime numbers? The latter is fine, and the Sieve is a fine answer if that is the case. If they asked the former, then they were just being arrogant. – riwalk Mar 16 '11 at 17:30
  • 2
    @Stargazer712: seriously, you'd walk out of an interview if they asked you whether or not your solution was the best? Why not just answer the question? For example, say "there are faster sieves, but this is the best I can code from memory". Or perhaps say, "I don't know, but then I'm not a number theorist". I don't think it's arrogant to want to know whether candidates can intelligently discuss their algorithm choices. – Steve Jessop Mar 16 '11 at 18:46
  • @Steve Jessop, It depends on the question. When I first read the question, I was under the assumption that they said, "Here's the Sieve of Eratosthenes. Can you improve it?" That question would be arrogant, as it demands a ridiculous amount of knowledge and you can't help but expect that people couldn't answer it from memory. As I said in my second comment, if they asked, "Write an algorithm to find all prime numbers up to N," and then followed up with, "Can you make it better?" then that would be fine. – riwalk Mar 16 '11 at 18:54
  • @Stargazer712: OK, I see, I misunderstood your second comment to mean they shouldn't ever ask if there was something better than SoE. I might say, "give me Google and half a day, I can probably give you an only slightly buggy Sieve of Atkin", just to show off that I'd actually heard of it :-) Or I might look at their Eratosthenes implementation, to see if they'd left out any of the obvious tweaks. But you're right, if they expect someone to produce a Seive of Atkin from memory in an interview, then either it's a very specialist role or they've a long search ahead of them... – Steve Jessop Mar 16 '11 at 19:04
  • @Stargazer: The interviewer dint ask me to prove SoE ... merely improve it or if there is another algo that beats it... Sorry for not being clear about the question. – tomkaith13 Mar 16 '11 at 19:16

3 Answers3

1

Well, it depends on what you mean by "best." The Sieve of Eratosthenes is very easy to implement, but the Sieve of Atkin will give you significantly better performance.

So, if "best" means easy to implement and understand, Eratosthenes is the way to go. If "best" means want to show off your skills as a mathematician or have a very fast algorithm, Atkin is the way to go.

jason
  • 220,745
  • 31
  • 400
  • 507
  • Optimized Atkin anyway. Definitely not the wikipedia variant. – IVlad Mar 16 '11 at 19:21
  • 4
    @IVlad, Even an optimized Sieve of Atkin (SoA) as in [Atkin and Bernstein's "primegen"](http://cr.yp.to/primegen.html) (or further optimized) doesn't beat an optimized Sieve of Eratosthenes (SoE) as in ["primesieve"](http://primesieve.org/) as per discussion in [my answer here](http://stackoverflow.com/a/22161595/549617). In short, Atkin and Bernstein's comparison study restricted the SoE to the same level of wheel factorization as inherent to the SoA which is much less than possible for SoE; further, they "cheated" using a page buffer size of 4 KB for SoE and 8 KB for SoA in the comparison. – GordonBGood Apr 01 '14 at 06:05
1

Well , it only depends on the value of N :

  • The sieve of Eratosthenes ( Simple Sieve ) is one of the most efficient algorithm to find all primes smaller than n when n is smaller than 10 million ( Means 10^7 ) because Simple sieve requires O(n) linear space. And we know that we can make a global array of maximum size 10^7. So , when n is larger than 10^7 , the Simple Sieve faces issue because an array of size more than 10^7 may not fit in memory.

  • For n>=10^7 , we can use Segmented Sieve of Eratosthenes because in segmented sieve , we can improve memory consumption from linear to O(√n) space.

Note that time complexity of Segmented Sieve is same as Simple Sieve. The only advantage which segmented sieve have : it is perfect for large ‘n’

Vishal Srivastav
  • 425
  • 4
  • 11
0

For a programming interview, no :). There's this though http://en.wikipedia.org/wiki/Sieve_of_Atkin and I'm sure there's probably research papers out there that eek out small optimizations.

dfb
  • 12,827
  • 1
  • 26
  • 52