Improved Sieve Method-prime number in prime number (prime number Sieve method)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.