Java黑皮书课后题第7章:*7.6(修改程序清单5-15)程序清单5-15通过检验2、3、4…n/2是否是数n的因子来判断n是否为素数。判断n是否素数的更高效的方法是检验小于等于根n的素数是否有n整
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第7章:*7.6(修改程序清单5-15)程序清单5-15通过检验2、3、4…n/2是否是数n的因子来判断n是否为素数。判断n是否素数的更高效的方法是检验小于等于根n的素数是否有n整
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
*7.6(修改程序清單5-15)程序清單5-15通過檢驗2、3、4…n/2是否是數(shù)n的因子來判斷n是否為素數(shù)。判斷n是否素數(shù)的更高效的方法改寫5-15
- 題目
- 題目描述
- 破題
- 程序清單5-15(非本題代碼)
- 本題代碼
- 運行示例
題目
題目描述
*7.6(修改程序清單5-15)程序清單5-15通過檢驗2、3、4…n/2是否是數(shù)n的因子來判斷n是否為素數(shù)。判斷n是否素數(shù)的更高效的方法是檢驗小于等于根n的素數(shù)是否有一個能被n整除。如果都不能則n就是素數(shù)。用這種方法改寫程序清單5-15以顯示前50個素數(shù)。需要使用一個數(shù)組存儲這些素數(shù),之后使用這些素數(shù)來檢測它們是否可能為n的因子
破題
本題關(guān)鍵點已經(jīng)圈畫
程序清單5-15(非本題代碼)
public class qingdan {public static void main(String[] args) {// Number of primes to display:輸出50個素數(shù)final int NUMBER_OF_PRIMES = 50;// Display 10 per line:一行10個數(shù)字final int NUMBER_OF_PER_LINE = 10;// Count the number of prime numbers:計數(shù)變量int count = 0;// A number to be tested for primeness:測試用變量int number = 2;System.out.println("The first 50 prime numbers are \n");// Repeatedly find prime numberswhile (count < NUMBER_OF_PRIMES) {// Assume the number is primeboolean isPrime = true; // Is the current number prime?// Test whether number is primefor(int divisor = 2; divisor <= number / 2;divisor++){if (number % divisor == 0){ // If true, number is not primeisPrime =false;break;}}// Display the prime number and increase the countif( isPrime ){count++; // Increase the countif(count % NUMBER_OF_PER_LINE == 0){// Display the number and advance to the new lineSystem.out.println(number);}elseSystem.out.print(number + " ");}// Check if the next number is primenumber++;}} }本題代碼
public class Test7_6 {public static void main(String[] args) {// Number of primes to display:輸出50個素數(shù)final int NUMBER_OF_PRIMES = 50;// Display 10 per line:一行10個數(shù)字final int NUMBER_OF_PER_LINE = 10;// Count the number of prime numbers:計數(shù)變量int count = 0;// A number to be tested for primeness:測試用變量int number = 2;// 設(shè)置一個接收素數(shù)的數(shù)組int[] primeList = new int[50];// Repeatedly find prime numberswhile (count < NUMBER_OF_PRIMES) {// Assume the number is primeboolean isPrime = true; // Is the current number prime?// Test whether number is primefor(int divisor = 2; divisor <= number / 2;divisor++){if (number % divisor == 0){ // If true, number is not primeisPrime = false;break;}}// Display the prime number and increase the countif( isPrime ){primeList[count] = number;count++; // Increase the countif(count % NUMBER_OF_PER_LINE == 0){// Display the number and advance to the new lineSystem.out.println(number);}elseSystem.out.print(number + " ");}// Check if the next number is primenumber++;}// 使用新方法判斷primeList[]中的素數(shù)(假設(shè)為n),有沒有比根n小的素數(shù)能被n整除// 只要有1個能被整除即可退出并輸出可能,如果全部遍歷完都不是的話,則輸出不可能int length = primeList.length;double root = 0;for (int i = length - 1 ; i >= 0 ; i--){// 求根下nroot = Math.sqrt(primeList[i]);// 遍歷<根下n的素數(shù),看是否能被n整除,如果是則輸出結(jié)果并強制退出for (int y = 0 ; primeList[y] < root ; y++){if (primeList[i] % primeList[y] == 0){System.out.println("可能");System.exit(0);}}}System.out.println("不可能");} }運行示例
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 不可能總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第7章:*7.6(修改程序清单5-15)程序清单5-15通过检验2、3、4…n/2是否是数n的因子来判断n是否为素数。判断n是否素数的更高效的方法是检验小于等于根n的素数是否有n整的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第7章:**7.5(
- 下一篇: Java黑皮书课后题第7章:*7.7(统