To find count of prime between a range

package learn_java;
import java.util.;
class A_class{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int c=1000000;
int a[]=new int [1000005];
int p[]=prime_sieve(a,c);
while(t–>0) {
int s=sc.nextInt();
int e=sc.nextInt();
System.out.println(p[e]-p[s-1]);
}
}
static int[] prime_sieve(int a[],int c) {
for(int i=3;i<c;i=i+2 ) {
a[i]=1;
}
for(int i=3;i<c;i++) {
if(a[i]==1) {
for(int j=i
i;j<c;j=j+i) {
a[j]=0;
}
}
}
a[0]=a[1]=0;
a[2]=1;
int p[]=new int[c+5];
for(int i=0;i<c;i++) {
if(i>0) {
p[i]=a[i]+a[i-1];

		}else {
			p[i]=a[i];
		}
	}
	return p;
}

}

Can you pls tell the logic you have used… since I m not able to get your solution…as wht approach you have used.

1- I created an array and marked all odd position as 1.
2-then i run a loop and marked all position which is at multiple of 3 ,…then multiple of 5 …then 7.
3-then i created an array and store count of prime till any number ex-20,30,70.

Use the following logic in your code as,

void primeseive(long long int p[1000000],long long int N)
{
p[0]=p[1]=0;
p[2]=1;
for(long long int i=3;i<N;i=i+2)
{
p[i]=1;
}
for(long long int i=3;i<N;i=i+2)
{
if(p[i])
{
for(long long int j=ii;j<N;j=j+2i)
{
p[j]=0;
}
}
}
return;
}

and in the main you can maintain as :

long long int N=1000000;
long long int p[N];
primeseive(p,N);

for(long long int i=a;i<=b;i++)
{
if(p[i]==1)
{
cnt=cnt+1;
}
}
cout<<cnt<<endl;

This is the basic approach you can follow in your code…

Hey @Affan-Mokarram-202031887744398
How can I help you ?

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.