题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here while(line = await readline()){ let n = parseInt(line); let half_v = n/2; // 只需要计算其二分之一即可 let small_v = 0, big_v = 0; for(let i=half_v; i>0;i--){ if(checkSushu(i) && checkSushu(n-i)){ small_v = i; big_v = n-i; break; } } console.log(small_v); console.log(big_v); } }() function checkSushu(n){ let flag = true; // 默认是素数 let i = 2; while(i<n){ if(n%i == 0){ flag = false; // 不是素数 } i++; } return flag; }