找出n以内的质数

/**
     * @des 找出n以内的质数  20
     * 2 3 5 7 9 11 13 15 17 19
     * @param n
     */
    private static void print(int n) {
        if (n<2){
           return;
        }
        System.out.print("2 ");
        for (int i = 3; i < n; i++) {
            if (i%2==0){
                continue;
            }
            boolean f = true;
            for (int j = 3; j < (int)Math.sqrt(i); j++) {
                if (i%j==0){
                    f = false;
                    break;
                }
            }
            if (f){
                System.out.print(i+" ");
            }
        }
        System.out.println();
    }

猜你喜欢

转载自www.cnblogs.com/wushenghfut/p/12637334.html