//按顺序打印出100个回文素数
public class PrimeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
for(int i = 2;count < 100;i++){
if(isPrime(i) && isPalindrome(i)){
System.out.print(i + " ");
count++;
if(count % 10 == 0)
System.out.println();
}
}
}
public static boolean isPrime(int num) { //判断是否为素数
for(int i = 2;i <= Math.sqrt(num);i++)
if(num % i == 0)
return false;
return true;
}
static int reversal(int num) { //反转原数
int result = 0;
while(num != 0){
int lastDigit = num % 10;
result = result * 10+ lastDigit;
num = num / 10;
}
return result;
}
static boolean isPalindrome(int num) { //判断是否为回文数
return num == reversal(num);
}
}
网友评论