The prime number in nod 1181 prime (prime number Sieve method)

Source: Internet
Author: User
Tags time limit
1181 prime number in prime number (prime number Sieve) topic Source: Sgu Base time limit: 1 second space limit: 131072 KB score: 0 Difficulty: Basic collection concern If a prime number is a primes, the numbers in the list are also prime numbers, so it is called prime numbers in prime numbers. 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
#include <bits/stdc++.h>
const int maxn=1e6+200;
using namespace std;
BOOL A[maxn]={false};
int main ()
{
    long long n,i,j;
	Long long ans=0;
	cin>>n;
	A[1]=true;
	for (i=2; i<maxn; i++)
	{
		if (!a[i])
		{
			ans++;
			if (!a[ans]&&i>=n)
			{
				cout<<i<<endl;
				break;
			}
			for (j=i*i; j<maxn; j+=i)
			a[j]=true;
		}
	}
	return 0;
}

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.