给定一个正整数,编写程序计算有多少对质数的和等于这个输入的正整数,并输出结果

Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("please input a positive integer: ");
            int n = sc.nextInt();
            if(n < 4) {
                break;
            }else if(n == 4) {
                System.out.println(n + " = 2 + "+(n-2));
            }
            for(int i = 3; i <= n/2; i += 2) {
                if(isPrimer(i) && isPrimer(n-i)) {
                    System.out.println(n + " = " + i + " + " + (n-i));
                }
            }
        }
    }
    public static boolean isPrimer(int n) {
        double max = Math.sqrt(n);
        if(n == 1) 
            return false;
        else if(n == 2 || n == 3)
            return true;
        else {
            for(int i = 2 ; i <= max; i++) {
                if(n % i == 0) 
                    return false;
            }
            return true;
        }
       
    }

 

posted @ 2018-11-17 21:43  束发读诗书  阅读(2147)  评论(0编辑  收藏  举报