Implement an algorithm to generate all prime number from 1-100 in fastest and most efficient way
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Showing posts with label prime. Show all posts
Showing posts with label prime. Show all posts
Jan 3, 2009
Dec 26, 2008
Write code to detect whether or not a given number is prime
Write code to detect whether or not a given number is prime
Better solution, see wiki
bool foo(int num)
{
if(num<=1) return false;
if(num==2) return true;
if(!num%2) return false;
for(int i=3;i < sqrt(num);i++)
{if(!num%i)return false;}
return true;
}
Better solution, see wiki
Subscribe to:
Posts (Atom)