使用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"); } }
相关文章推荐
- 使用java代码实现求某个范围内的所有素数
- 使用java写一个没有main方法的“Hello World”程序(摘自java核心技术.卷I)
- 使用java.util.BitSet 求素数的算法
- 使用java反射技术完成对象所有属性值的输出
- JAVA计算器——摘自Java核心技术
- ImportNew 首页所有文章资讯Web架构基础技术书籍教程我要投稿更多频道 » 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- 使用java反射技术完成对象所有属性值的输出
- 面向对象类间关系 摘自【java核心技术 卷1 基础知识 原书第8版】
- 利用JAVA反射技术查找一个类的所有属性
- [基础] Java目录(摘自Java核心技术·卷1 基础知识)
- 利用JAVA反射技术查找一个类的所有属性
- Java使用递归遍历查找所有的文件
- 37 java技术体系基础、tomcat核心概念及使用初步、tomcat配置与应用
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- Java多线程编程核心技术--Lock的使用(一)
- 显示当前月的日历(摘自java核心技术.卷I)
- 12.Java核心技术—static、final修饰符的使用
- 【Java多线程编程核心技术】4.Lock的使用-笔记总结
- Java多线程编程核心技术--Lock的使用(一)
- Java多线程编程核心技术(第四章Lock的使用)