随笔-16  评论-0  文章-0  trackbacks-0

质数算法
   这个质数算法比较新颖,效率也比较高.
class Primes
  {
    public static void main(String[] args)
      { int N = Integer.parseInt(args[0]);
        boolean[] a = new boolean[N];
        for (int i = 2; i < N; i++) a[i] = true;
        for (int i = 2; i < N; i++)
          if (a[i] != false)
            for (int j = i; j*i < N; j++)
              a[i*j] = false;
        for (int i = 2; i < N; i++)
          if (i > N - 100)
            if (a[i]) Out.print(" " + i);
        Out.println();
      }
  }
  

posted on 2006-10-16 18:51 尨奇 阅读(254) 评论(0)  编辑  收藏 所属分类: algorithms in java