您的位置:首页 > 编程语言 > Java开发

使用BitSet查找某个范围内的所有的素数的个数(摘自java核心技术.卷I)

2013-08-30 10:30 344 查看
import java.util.*;

/**
* This program runs the Sieve of Erathostenes benchmark. It computes all primes up to 2,000,000.
* @version 1.21 2004-08-03
* @author Cay Horstmann
*/
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");
}

}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐