1181 prime numbers in prime numbers (prime number Sieve method)

Source: Internet
Author: User

1181 prime numbers in prime numbers (prime number Sieve method)

Base time limit: 1 seconds space limit: 131072 KB If a prime number is a prime in a list of numbers, 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
#include <cstdlib>#include<map>#include<cstring>#include<iostream>intMain () {int*Is_prime; Is_prime= (int*)malloc(sizeof(int) *10010000); is_prime[0]= is_prime[1]=1;  for(intI=2; i<10010000; i++)    {        if(!Is_prime[i]) for(intJ=i+i; j<10010000; j+=i) {is_prime[j]=1; }} std::map<int,int>m; intCnt=1;  for(intI=1; i<=10010000; i++)    {        if(!Is_prime[i]) {M[i]= cnt++; }    }    intN;  while(std::cin>>N) {intRec;  for(intI=n;; i++)        {            if(!is_prime[i] &&!Is_prime[m[i]]) {Rec=i;  Break; }} std::cout<< rec<<'\ n'; }     Free(Is_prime); Is_prime=NULL; return 0;}

1181 prime numbers in prime numbers (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.