题解 | #查找组成一个偶数最接近的两个素数#

查找组成一个偶数最接近的两个素数

http://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9

while(line = readline()) {
    let num = parseInt(line);
    let diff = 9999999999;
    for(let start=1; start <= num/2; start++) {
        for (let end=Math.floor(num/2); end < num; end++) {
            if (start + end == num && isZhi(end) && isZhi(start)) {
                if (end - start < diff  ) {
                    diff = end - start;
                }
            }
        }
    }

    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;
    } 
}



全部评论

相关推荐

拉丁是我干掉的:把上海理工大学改成北京理工大学。成功率增加200%
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务