`
BradyZhu
  • 浏览: 250242 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

软件大赛题目----(第六个)计算孪生素数

 
阅读更多




代码如下

package com.bird.software;

/**
 * 
 * @author Bird
 *
 */
public class Test6 {
	public static boolean isPrime(int x){
		for(int i = 2; i <= x/2; i++){
			if(x%i==0)return false;
		}
		return true;
	}
	
	
	public static int twinPrimeNum(int n){
		int sum = 0;
		for(int i = 2; i < n; i++){
			if(isPrime(i)&&isPrime(i+2))
				sum++;
		}
		return sum;
	}
	
	
	public static void main(String[] args){
		System.out.println(twinPrimeNum(100));
	}
}


运行结果如下
8


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics