The number of prime numbers by Euler sieve method

Source: Internet
Author: User

Determine whether a is a prime number, and find 1--n of prime numbers

Consider the Euler sieve method ————

Http://wenku.baidu.com/link?url=dFs00TAw8_ K46aesbxy5nb5lvqj51uujgy9zvwedqdwjln-qlfwzuycgpe5edcztnqamtkfubssebvfbzv4fcqvlneovhjjqvgjjcgc1in7

//Determines whether the number is a prime number, or 1 to n#include <iostream>#include<cstring>#defineMAX 10000000using namespacestd;BOOLb[max+1];//b 0 is prime, 1 is non-primeintc[max+1];intSushu (intN) {memset (b,0, n +1); b[0]=1; b[1]=1;//1 non-prime and non-composite    intI, j, cnt =0, T;  for(i =2; I <= N; i++){        if(!B[i]) {c[cnt++] =i; }         for(j =0; j<cnt; J + +) {T= I*c[j];//prime numbers multiply prime numbers for composite, prime numbers multiply composite for composite, a composite must be written as prime numbers in the form of a number            if(T >N) Break; B[t]=true; if(I%c[j] = =0)                 Break; }    }    returnCNT;}intMain () {intT,a,n,c; CIN>>u;  while(t--) {cin>>a>>N; C=Sushu (n); if(b[a]==0) cout<<"YES"<<c<<Endl; Elsecout<<"NO"<<c<<Endl; }    return 0;}

Look at someone else's code for 1 billion seconds, and

#include <iostream>#include<cstring>#include<cstdio>#include<ctime>#include<algorithm>using namespacestd;intn=11000000;BOOLvisit[11000000];intprime[11000000];voidInit_prim () {memset (visit,true,sizeof(visit)); visit[0] = visit[1] =false; intnum =0;  for(inti =2; I <= N; ++i) {if(Visit[i] = =true) {num++; Prime[num]=i; }        /*For (int j = 1;  ((J <= num) && (i * prime[j] <= n));            ++J) {Visit[i * Prime[j]] = false;        if (i% prime[j] = = 0) break; }*/         for(intj =2; I *j <=n; J + +) {Visit[i* j] =false; }    }}intOutnum (intN) {    inti;  for(i=1;p rime[i]<=n;i++); returnI-1;}intMain () {clock_t start, finish; Doubleduration; Start=clock ();    Init_prim (); intT; CIN>>T;  while(t--)    {        intX,n; CIN>> x >>N; if(Visit[x]) cout<<"YES"<<" "; Elsecout<<"NO"<<" "; cout<< outnum (n) <<Endl; } Finish=clock (); Duration= (Double) (Finish-start)/clocks_per_sec*1000.0; printf ("%f ms\n", duration); return 0;} 

The number of prime numbers by Euler 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.