1181 prime numbers in prime numbers (prime number Sieve method)
Title Source: Sgu
Base time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic problem
If a prime number is a prime in the mass list, it is called prime in prime. For example: 3 5 are prime numbers for the 2nd and 3rd respectively, so they are prime numbers in prime numbers. Now give a number n, and find out how many prime numbers are in the smallest prime numbers of >=n (which can be considered by the prime sieve method).
Input
Enter a number n (n <= 10^6)
Output
The output >=n the prime number in the smallest prime number.
Input example
20
Output example
31
The most common screening method is a multiple of the number of sieve primes, but it is clear that the same elements will be sifted out in large numbers (albeit better than the multiples of the sieved integers). And by the definition of composite can be seen, composite must have a complex number of elements, we only need to judge at the first factor, otherwise it will be repeated.
This problem needs to be judged on the basis of the Sieve method.
"The idea comes from http://blog.csdn.net/morewindows/article/details/7347459."
The code is as follows
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace Std;
BOOL su[100000000];
int shu[2000500],shusu[2000500];
int shai (int a) {
int i=2,j,k=1,l=1;
while (Shusu[l-1]<a) {
if (!su[k-1]&&shusu[l-1]!=shu[k-1])
SHUSU[L++]=SHU[K-1];
if (!su[i]) shu[k++]=i;
for (j=1;j<=k&&i*shu[j]<=a*10;j++)
{
Su[i*shu[j]]=true;
if (i%shu[j]==0) break;
}
i++;
}
l--;
return l;
}
int main ()
{
int n;
cin>>n;
for (int I=1;i<=shai (n); i++) cout<<shusu[i]<< "";
Cout<<shusu[shai (n)]<<endl;
return 0;
}
Improved Sieve Method-prime number in prime number (prime number Sieve method)