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: ???
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.