Answer to Question #18389 in C++ for Ige

Question #18389
how to write a C++ program to find all the prime numbers between 1 and 20000
1
Expert's answer
2012-11-13T10:28:57-0500
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <time.h>
using namespace std;
#include <vector>
int Eratosfen (int n)
{
vector <bool> pA (n+1); //масив використовується з індекса 2
for (int i=0 ; i<n ; ++i)
& pA[i]=0;
int count = n-1;
for (int i=2; i*i<=n ; i++)
{
&
& if (pA[i]==0)
& for (int j=i*i; j<=n ; j+=i)
& if (pA[j]==0)
& {
&
& pA[j]=1;
& count--;
& }
}
for (int i = 0 ; i < pA.size() ; i++){
& if (pA[i] == false) cout<<i<<endl;
}
return count;
}

int main ()
{
Eratosfen(20000);
system ("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS