java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter6-27
自己纯手工,欢迎讨论
package chapter6;
public class T27 {
public static void main(String[] args) {
// 测试翻转
// System.out.println(reverse(119));
// 测试素数
// System.out.println(isPrime(59));
print();
}
public static void print() {
int i = 0;
int j = 2;
while (i < 100) {
if (isPrime(j) && isPrime(reverse(j))) {
System.out.print(j + "\t");
i++;
if (i % 10 == 0)
System.out.println();
}
j++;
}
}
//判断素数
public static boolean isPrime(int n) {
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
//翻转函数
public static int reverse(int n) {
int sum = 0;
while (n / 10 != 0) {
sum = sum * 10 + n % 10;
n /= 10;
}
return sum = sum * 10 + n;
}
}