import java.util.Scanner;
public class LuckyNumbers {
static int T,ans[];
static long A,B;
public static void main(String ar[]){
Scanner scan=new Scanner(System.in);
T=scan.nextInt();
ans=new int[T];
for(int i=0;i<T;i++){
A=scan.nextLong();
B=scan.nextLong();
for(long j=A;j<=B;j++){
if(getLucky(j)){
ans[i]++;
}
}
}
for(int i=0;i<T;i++){
System.out.println(ans[i]);
}
}
public static boolean getLucky(long j){
boolean lucky=false;
long rem,sum=0,sum1=0;
while(j>0){
rem=j%10;
sum=sum+rem;
sum1=sum1+(rem*rem);
j=j/10;
}
if(isPrime(sum)&&isPrime(sum1)){
lucky=true;
}
return lucky;
}
public static boolean isPrime(long sum){
boolean status=true;
if(sum!=1){
for (int i=2; i < sum ;i++ ){
long n =sum % i;
if (n==0){
status=false;
break;
}
}
}else{
status=false;
}
return status;
}
}
This code is for the problem in which i'm finding a total number of numbers that exist beetween A and B, of which sum of digits and sum of square of digits are prime. But I need to make it Optimal. How Can i do so?