This is Discussion thread about Prime Visits
Discussion About Prime Visits
package HackerBlocks;
import java.util.Scanner;
public class PrimeVisits {
public static void main(String[] args) {
int times = 0;
Scanner scan=new Scanner(System.in);
if(scan.hasNextInt()) {
times=scan.nextInt();
}
while(times>0) {
int a=scan.nextInt();
int b=scan.nextInt();
int count=0;
int i=a;
if(a==0||a==1) {
i=2;
}
for(;i<=b;i++) {
int flag=0;
for(int j=2;j<i;j++) {
if(i%j==0) {
flag=1;break;
}}
if(flag==0) {
count++;
}
}
System.out.println(count);
times--;
}
}
}
help me in test case3. it is showing me Time limit exceed error
Basically we need to find count of prime no.s between a and b.
finding prime no.s using traditional approach will give TLE.
Using sieve of eratosthenes we can precompute primes.