提交 99fdaa44 编写于 作者: 邓某人的父亲's avatar 邓某人的父亲

上传新文件

上级 d9ecce2e
import java.util.*;
/**
* This program runs the Sieve of Eratosthenes benchmark. It computes all primes
* up to 2,000,000.
*
* @author Cay Horstmann
* @version 1.21 2004-08-03
*/
public class Sieve {
public static void main(String[] s) {
int n = 2000000;
long start = System.currentTimeMillis();
BitSet b = new BitSet(n + 1);
int count = 0;
int i;
for (i = 2; i <= n; i++)
b.set(i);
i = 2;
while (i * i <= n) {
if (b.get(i)) {
count++;
int k = 2 * i;
while (k <= n) {
b.clear(k);
k += i;
}
}
i++;
}
while (i <= n) {
if (b.get(i))
count++;
i++;
}
long end = System.currentTimeMillis();
System.out.println(count + " primes");
System.out.println((end - start) + " milliseconds");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册