-
Notifications
You must be signed in to change notification settings - Fork 17
/
prime Visit.cpp
52 lines (48 loc) · 1.05 KB
/
prime Visit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void prime_sieve(int *p)
{ // we'll skip all even numbers.
//then mark all odd numbers as (potential)prime.
//mark them as 1.
for(int i=3;i<=1000;i+=2)
{p[i]=1;}
//now the seive
//we will jump over all odd numbers.
//here we'll check that if the current no is
//not marked then its prime.
//then we'mark all its multiple starting from its square.
//1->prime 0->not prime
for(ll i=3;i<=100005;i+=2)
{
if(p[i]==1)
{
for(ll j=i*i;j<=100005;j=j+i)
{
p[j]=0;
}
}
}
p[2]=1;
p[1]=p[0]=0;
}
int main()
{ int n;
//cout<<"Enter the value of n:";
// cin>>n;
int p[100005]={0};
prime_sieve(p);
//printing prime upto n.
int csum[100005]={0};
for(int i=1;i<=100005;i++)
{csum[i]=csum[i-1]+p[i];}
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
cout<<csum[b]-csum[a-1]<<"\n";
}
return 0;
}