import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
boolean[] primes = new boolean[61];
for (int i = 2; i < 61; i++) {
primes[i] = isPrime(i);
}
int[] range = new int[1000 * 1000 + 1];
int temp = 0;
for (int i = 0; i < 1000 * 1000 + 1; i++) {
if (isPrime(i)) {
if (primes[numtoInt(i)]) {
temp++;
}
}
range[i] = temp;
}
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int cases = Integer.parseInt(br.readLine());
StringBuilder out = new StringBuilder("");
for (int i = 0; i < cases; i++) {
StringTokenizer m=new StringTokenizer(br.readLine());
int x = Integer.parseInt(m.nextToken());
int y = Integer.parseInt(m.nextToken());
int counter = range[y]-range[x-1];
out.append(counter);
out.append("\n");
}
System.out.print(out);
}
static int numtoInt(int x) {
if (x > 9) {
return x % 10 + numtoInt(x / 10);
}
return x % 10;
}
static boolean isPrime(int x) {
if (x == 2) {
return true;
}
for (int i = 2; i < Math.sqrt(x) + 1; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
No comments:
Post a Comment