Description
素数又称质数。指一个大于1的自然数,除了1和此整数自身外,不能被其他自然数整除的数。我们定义:如果一个素数是完美的素数,当且仅当它的每一位数字之和也是一个素数。现在给你一个正整数,你需要写个程序判断一下这个数按照上面的定义是不是一个完美的素数。
Input
输入包含多组测试数据。
每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
Output
对于每组测试数据,如果 n 是完美的素数,输出“YES”,否则输出“NO”(输出均不含引号)。
Sample
Input
11
13
Output
YES
NO
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n, sum;
while (reader.hasNext()) {
sum = 0;
n = reader.nextInt();
int a = n;
if (isPrime(n)) {
while (true) {
sum += a % 10;
a /= 10;
if (a == 0)
break;
}
if (isPrime(sum)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else
System.out.println("NO");
}
reader.close();
}
private static boolean isPrime(int n) {
boolean flag = true;
if (n == 1)
flag = false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
flag = false;
break;
}
}
return flag;
}
}