To find the nth number of primes

Source: Internet
Author: User

Test instructions

Enter a number n (n <= 200000) and output the bottom n primes.

Ideas:

There are about 210000 primes in 1-3000000. Direct these primes to the table, then the offline query on OK.

Code:

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <algorithm>7#include <queue>8#include <stack>9#include <vector>Ten using namespacestd; OnetypedefLong LongLL; A  - BOOLvisit[3010000+7];  - intprime[3000000+7];  the intans[220000+7]; - voidInit_prim () { -memset (Visit,true,sizeof(visit));  -     intnum =0;  +      for(inti =2; I <=3000000; ++i) {   -         if(Visit[i] = =true){   +num++;  APrime[num] =i;  at         }   -          for(intj =1; ((J <= num) && (i * prime[j] <=3000000)); ++j) {   -Visit[i * Prime[j]] =false;  -             if(i% prime[j] = =0) Break;  -         }   -     }   in } -  to intMain () + { -     //freopen ("Input.txt", "R", stdin); thememset (Prime,0,sizeof(prime)); * Init_prim (); $     intCNT =0;Panax Notoginseng      for(inti =0; I <=3000000; i++){ -         if(Prime[i]) ans[cnt++] =Prime[i]; the     } +     intT; Ascanf"%d", &t); the      while(t--){ +         intN; -scanf"%d", &n); $printf"%d\n", Ans[n-1]); $     } -     return 0; -}

To find the nth number of primes

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.