一个数如果从左往右读和从右往左读数字是完全相同的,则称这个数为回文数,比如898,1221,15651都是回文数。编写一个程序,输入两个整数min和max,然后对于min~max之间的每一个整数(包括min和max),如果它既是一个回文数又是一个质数,那么就把它打印出来。要求,回文数和质数的判断都必要要用函数的形式来实现。
输入:
5 100
输出:
5 7 11
public class Wu {
public static void main(String args[]) throws Exception
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
for(int i=m;i<=n;i++){
if(i>1) {
boolean bool=true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
bool = false;
break;
}
}
if (bool) {
String a = String.valueOf(i);
char[] ch = a.toCharArray();
int first = 0;
int last = ch.length - 1;
while (first < last) {
if (ch[first] != ch[last]) {
bool = false;
break;
}
first++;
last--;
}
}
if (bool) {
System.out.print(i + " ");
}
}
}
}
}