import java.util.Scanner;
/**
* 回文(去掉一个字符)素数
* input:输入两个数n<m
* output:输出两个数之间回文(去掉一个字符)素数的个数
* 回文素数:对一个数去掉一位之后是回文素数(既是素数又是回文数)
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.close();
int val = print(n, m);
System.out.println(val);
}
public static int print(int n, int m) {
int count = 0;
for (int i = n; i <= m; i++) {
if (isPrime(i)) {
if (isHuiWenDel(Integer.toString(i))) {
count++;
}
}
}
return count;
}
/**
* 是否素数
*/
public static boolean isPrime(int num) {
boolean prime = true;
if (num == 1) prime = false;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
/**
* 是否回文数
*/
public static boolean isHuiWen(String string) {
if (Integer.valueOf(string) == 0 || Integer.valueOf(string) == 1) { // 处理0 1
return false;
}
StringBuffer stringBuffer = new StringBuffer(string);
StringBuffer stringBufferReverse = stringBuffer.reverse();
return stringBuffer.equals(stringBufferReverse) ? true : false;
}
/**
* 去掉一个数字是否回文数
*/
public static boolean isHuiWenDel(String strings) {
boolean flag = false;
// 去掉一个字符
int length = strings.length();
for (int i = 0; i < length - 1; i++) {
String s = strings.substring(0, i) + strings.substring(i + 1);
if (isHuiWen(s)) {
flag = true;
}
}
return flag;
}
}
回文(去掉一个字符)素数
最新推荐文章于 2022-05-08 17:16:22 发布