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

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

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

import java.util.*;

public class Main {

    public Main() {
    }

    private boolean isPrime(int num) {
        for (int i = 2; i <= num/i; i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public int count(int n) {
        int i = n/2, j = n - i;
        while (!isPrime(i) || !isPrime(j)) {
            i++;
            j--;
        }
        return j;
    }

    public static void main(String[] args) {
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.next());
            int res = solution.count(n);
            System.out.println(res);
            System.out.println(n - res);
        }
    } 
}
全部评论
i<=Math.sqrt(num)
1 回复 分享
发布于 2022-06-01 22:31
for (int i = 2; i <= num/i; i++) 应该写成i <= num/2吧?
点赞 回复 分享
发布于 2022-03-03 14:48

相关推荐

饼子吃到撑:学院本是这样的,找工作拼运气,你技术再好人家筛选学历照样沉入海底,海投就行了
点赞 评论 收藏
分享
评论
24
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务