(3,5)
(5,7)
...
public class C1 { public static void main(String[] args) { int count = 1; int[] prime = new int[2]; for(int i = 1 ;i <= 1000 ; i++ )//設定範圍從1-1000 { boolean a = isPrimeNumber(i);//呼叫 isPrimeNumber 判斷是否為質數 if(a == true && count == 1 ) { prime[0] = i ; count = count + 1; } else if(a == true && count == 2 ) { prime[1] = i ; count = count + 1; } else if(a == true && count > 2) { System.out.println("(" + prime[0] + "," + prime[1] + ")" );//印出答案 prime[0] = prime[1];//印完後往前挪一個 prime[1] = i;//新找到的數字寫近來 } } } public static boolean isPrimeNumber(int number) { boolean bool = false ; int remainder = 0; int factor = 0 ; for(int i = 1 ; i <= number ; i++)//計算此數的因數個數 { remainder = number % i; if(remainder == 0) { factor = factor + 1; } } if(factor == 2) { bool = true ; remainder = 0; factor = 0 ; } else if(factor != 2) { bool = false ; remainder = 0; factor = 0 ; } return bool ; } }
沒有留言:
張貼留言