Number of primes in interval (also used with the algorithm)

Source: Internet
Author: User
Tags square root

Question: Given positive integers a and B, how many primes are there in the interval [A, a)

Restrictions: a<b<=10^12 b-a<=10^6

Examples:

22 37 3

22801763489 22801787297 1000

Train of thought: because the minimum factorization of composite within B must not exceed the square root B, if there is a prime table within the square root B, you can use the wagered algorithm on [a, b]. In other words, the table of [2, square root b] and the table of [A, a, a] are done separately.

And then from [2, square root b] in the table of the Sieve prime, but also its multiples from the [A, a] table, the last remaining is [a, b] within the prime.

Look at the code

#include <iostream>#include<string.h>#include<map>#include<cstdio>#include<cstring>#include<stdio.h>#include<cmath>#include<math.h>#include<algorithm>#include<Set>#include<queue>typedefLong Longll;using namespacestd;Constll mod=1e9+7;Const intmaxn=1e6+Ten;Const intmaxk=5e3+Ten;Const intmaxx=1e4+Ten;Constll maxe= ++Ten;#defineINF 0x3f3f3f3f3f3f#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1ll L,r;BOOLIS_PRIME[MAXN],IS_PRIME_SMALL[MAXN];voidsolve () {ll sum=0;  for(LL i=0; i*i<r;i++) is_prime_small[i]=false;  for(LL i=0; i<r-l;i++) is_prime[i]=false;//the scope of the topic is too large, it is impossible to open such a large array, so use R-l to store     for(LL i=2; i*i<r;i++)    {        if(!Is_prime_small[i]) {           for(LL j=2*i;j*j<r;j+=i) is_prime_small[j]=true;//Table of filter [2, square root b]           for(ll J=max (i*2ll,l%i==0? L: (l/i+1) *i); j<r;j+=i)//why is Max (i*2ll,l%i==0?l: (l/i+1) *i), because it could be the first i*2, or maybe the back i*n*2, which looks at the value of L          {              if(!is_prime[j-L]) {is_prime[j-l]=true; //cout<<j<< "";sum++; }          }        }    }   //cout<<endl;    if(l==1) sum++;//Look, we need a special 1 .if (l==0)
{
if (r==1) sum+=1;
Else sum+=2;//Special Award 0
}
cout<<r-l-sum<<Endl;}intMain () {CIN>>l>>R; Solve (); return 0;}

Number of primes in interval (also used with the algorithm)

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.