Sunday, July 02, 2006

[c++] Source code to find Prime numbers < n+1

/*Code is written by Vaibhav Gupta*/

int prime(int n){

bool *prime = new bool[n+1];
int i;
for(i=0;i<n+1;i++){
prime[i] = true;
}
prime[0]=false;
prime[1]=false;
int m = (int)sqrt((float)n);

for (i=2; i<=m; i++)
if (prime[i])
for (int k=i*i; k<=n; k+=i)
prime[k]=false;

for (i=0; i<n+1; i++){
if (prime[i]) cout << i << " ";
}
cout << endl;
return 0;
}