Prime number refers to the number can only be divided exactly by 1 and itself, so starting from the minimum number of decimal multiples screen out, the rest is for prime Numbers,
How could that be effective right sieve method,
First from given sorting from the beginning of the minimum number, multiples are losing it, circulation in turn until all the number of traverse,
The code below
#include
#include
using namespace std;
# define Max 100000
Char p (Max);
Int main ()
{
For (int I=2; i<=Max; I++)
P [I]=1;
For (int I=2; i<=Max; I++)
For (int j=2 * I; j<=Max; J=j + I)
P [j]=0;
For (int I=2; i<=Max; I++)
If (p [I])
coutreturn 0;
}
Of course, in multiples of four and two multiples, so add a judgment conditions, will be executed cycle for the effective operation of
#include
#include
using namespace std;
# define Max 100000
Char p (Max);
Int main ()
{
For (int I=2; i<=Max; I++)
P [I]=1;
For (int I=2; i<=Max; I++)
If (p [I])
For (int j=2 * I; j<=Max; J=j + I)
P [j]=0;
For (int I=2; i<=Max; I++)
If (p [I])
coutreturn 0;
}
CodePudding user response:
Said don't understand your sieve method, to determine the useful?CodePudding user response:
This is the problem? You? What is the problemCodePudding user response:
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html