Today, we need to find a suitable prime number, such as mental computing, pen computing, and online tools? It may be easier to write it by yourself. It can be considered a leisure and entertainment experience during work. The code is simple and the performance is poor. We do not recommend that you calculate more than 100,000 of the quality.
View plain
# Include <vector>
# Include <conio. h>
Void Prime (int max)
{
Std: vector <bool> isNotPrimes (max + 1 );
IsNotPrimes [0] = true;
IsNotPrimes [1] = true;
IsNotPrimes [2] = false;
For (int I = 2; I * 2 <= max; I ++)
{
For (int j = 2; j ++)
{
Int product = I * j;
If (product> max)
{
Break;
}
Else
{
IsNotPrimes [product] = true;
}
}
}
For (int I = 0, j = 0, count = isNotPrimes. size (); I <count; I ++)
{
If (! IsNotPrimes [I])
{
Printf ("% 4d", I );
J ++;
If (j % 10 = 0)
{
Printf ("\ n ");
}
}
}
}
Int main (int argc, char * argv [])
{
If (argc! = 2)
{
Printf ("% s <max> calculate 0 ~ Prime numbers between max \ n ", argv [0]);
_ Getch ();
Return 0;
}
Int max = atoi (argv [1]);
If (max <2)
{
Printf ("no result \ n ");
_ Getch ();
Return 0;
}
Prime (max );
_ Getch ();
Return 0;
}
From: Guo yuefeng's column