素數與哥德巴赫猜想

<pre name="code" class="java">package xuan.demo;
/**
 * @author xuan 素數
 */
public class 素數 {
	public static void main(String[] args) {
//		printNums(1000);//傳入打印範圍
//		System.out.println(whether(997));//傳入判斷數字
		printResult(8);
	}
	/**
	 * 打印素數
	 */
	public static void printNums(int count) {
		int i = 2, j;
		for (; i < count; i++) {
			for (j = 2; j < i; j++) {
				if (i % j == 0) {
					break;
				}
			}
			if (i == j) {
				System.out.print(i + "	");
			}
		}
	}

	/**
	 * 判斷是否爲素數
	 */
	public static boolean whether(int num) {
		int j;//此變量保存2到判斷數字的枚舉
		boolean isok = false;
		for (j = 2; j < num; j++) {
			//num不能整除j進入下次循環
			if (num % j == 0) {
				break;
			}
		}
		if (num == j) {
			isok = true;
		}
		return isok;
	}
	/**
	 * 哥德巴赫猜想:分解
	 */
	public static void printResult(int num){
		boolean isok = true;
		if (num%2==0) {
			int number = 0;
			for (int i = 0; i < num; i++) {
				for (int j = 0; j < num; j++) {
					number=i+j;
					if (number==num) {
						if (whether(i)&&whether(j)) {//如果兩個都是素數
							isok = false;
							System.out.println("素數和:"+num+"="+i+"+"+j);
						}else{
							System.out.println("非素數和:"+num+"="+i+"+"+j);
						}
					}
				}
			}
			if (isok) {
				System.out.println("恭喜你,凌駕於愛因斯坦,霍金等朱多牛逼人士之上,快去美國領100萬美元吧!");
			}else{
				System.out.println("親,您未能成功挑戰哥德巴赫猜想~~~~~~");
			}
		}else{
			System.out.println("輸入的爲奇數!");
		}
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章