互评作业:使用数组

用“埃氏筛法”求2~10000以内的素数。2~10000以内的数,先去掉2的倍数,再去掉3的倍数,再去掉4的倍数,……依此类推,最后剩下的就是素数。

 

Code:

class week3 {
    public static void main(String[] args) {
        System.out.println("Output the prims numbers in 2 ~ 10000:");
        int[] arr = new int[10005];

        for (int i = 2; i <= 10000; ++i) {
            if (arr[i] == 0) System.out.printf("%d ", i);
            for (int j = i; j <= 10000; j += i) arr[j] = 1;
        }

        System.out.println();
    }
};

  

 

posted @ 2019-03-06 15:54  Veritas_des_Liberty  阅读(274)  评论(0编辑  收藏  举报