Answer:
Several solutions exist, the basic solution is to use the sieve technique.
Basic Solution:
prime_nos = [1,2]
bas
for ( i = 3 to n )
{
if (i is divisible by any number < sqrt(i))
{
then i is not prime
i += 2; // increment by two, optimization, since even numbers
// cannot be prime.
}
else
{
add i to the prime_nos list.
}
}
Well known algorithms:
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
http://en.wikipedia.org/wiki/Sieve_of_Atkin
Also as per wikipedia:
One method for determining whether a number is prime is to divide by all primes less than or equal to the square root of that number. If any of the divisions come out as an integer, then the original number is not a prime. Otherwise, it is a prime. One need not actually calculate the square root; once one sees that the quotient is less than the divisor, one can stop.
No comments:
Post a Comment