题目:
*在质数中,若两个质数之差为2,我们称之为孪生质数,
* 例如(3、5)(5、7),输入2个正整数,
* 判断他是不是孪生质数,输出YES或者NO。
做法:
class Test56 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x1 = input.nextInt();
int x2 = input.nextInt();
if ((ifPrimeNumber(x1) && ifPrimeNumber(x2)) && (x1 - x2 == 2 || x2 - x1 == 2)){
System.out.println("YES");
}else {
System.out.println("NO");
}
input.close();
}
//判断一个数字,是否是 "质数"
// true = 质数
//false = 非质数
public static boolean ifPrimeNumber(int number) {
if (number == 1 || number == 0) {
return false;
}
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
标签:孪生,int,质数,number,x2,input,x1
From: https://www.cnblogs.com/chen-zhou1027/p/17466540.html