题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
while(line = readline()) {
let num = parseInt(line);
// x + y = num && y - x = diff
let diff;
for (let i=0; i<= num/2; i++) { //两个数差值最小为0,最大不会超过1/2,只要通过二元一次方程就能得出两个值,并保证为整数
let x = (i + num) / 2;
let y = (num - i) / 2;
if (Math.floor(x) == x && Math.floor(y) == y) {
if (isZhi(x)&&isZhi(y)) {
diff = i;
break;
}
}
}
let x = (diff + num) / 2;
let y = (num - diff) / 2;
print(y+'\n'+x)
function isZhi(count) {
let status = true;
for (let i=2; i<=count/2; i++) {
if (count%i == 0) {
status = false;
}
}
return status;
}
}