prime counting/质数计算
2012-11-07 12:57
246 查看
public class Prime { public int getPrime(int num){ boolean isprime[] = new boolean[num+1]; int count =0 ; for(int i=0; i<num+1; i++){ isprime[i] = true; } for(int i=2; i*i< num+1; i++){ if(isprime[i] == true){ for(int j=i; j*i<num+1; j++){ isprime[j*i] = false; } } } for(int i=2; i<num+1; i++){ if(isprime[i] == true) count++; } return count; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Prime().getPrime(1000)); //should be 168 } }
思路:
通过不断将质数的整数倍得到的结果标记为非质数来实现。这里使用了 Sieve of Eratosthenes
相关文章推荐
- 500以内质数(素数)的计算
- <NOIP> 25 . P1217 [USACO1.5]回文质数 Prime Palindromes
- 【cqbzoj】:1330 Prime DP(Ahio2001 质数和分解)
- USACO-Section1.5 Prime Palindromes [回文数][质数]
- 今天开始学Java 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。
- 【USACO1.5】回文质数 Prime Palindromes
- Lab 1 质数计算算法复杂度
- 计算质数-埃里克森筛法(间隔黄金武器)
- 欧拉计划之题目10:计算两百万以下所有质数的和。
- luogu P1217 [USACO1.5]回文质数 Prime Palindromes
- 质数和分解(prime)
- 【SPOJ-PRIME1】Prime Generator【区间质数筛】
- Java多线程计算某个范围内的质数
- PLSQL计算质数
- 一个数是否由两个质数组成——SGU113 Nearly prime numbers
- 一个计算质数的小程序。
- HDU 2138 How many prime numbers 质数个数
- Leetcode204. 埃拉托色尼法计算质数个数
- 判断质数 How many prime numbers
- hdu - 6096 Counting Divisors (数论计算约数的个数+思路)