第七章第六题(修改程序清单5-15)(Modify program list 5-15)
-
*7.6(修改程序清单5-15)程序清单5-15通过检验2,3,4,5,6,…,n/2是否是数n的因子来判断n是否是素数。如果找到一个因子,n就不是素数。判断n是否素数的另一个更高效的方法是:检验小于等于根号n的素数是否有一个能被n整除。如果都不能,则n就是素数。使用这个方法该些程序清单5-15以显示前50个素数。需要使用一个数组存储这些素数,之后使用这些素数来检测他们是否可能为n的因子。
*7.6(Modify program list 5-15)Listing 5-15 determines whether n is a prime by checking whether 2, 3, 4, 5, 6,…, N / 2 is a factor of n. If we find a factor, n is not a prime. Another more efficient way to judge whether n is a prime is to check whether one of the prime numbers less than or equal to the root sign n can be divisible by n. If not, then n is a prime. Using this method, the programs listing 5-15 show the first 50 primes. You need to use an array to store these primes, and then use them to detect whether they might be factors of n. -
参考代码:
package chapter07; public class Code_06 { public static void main(String[] args){ int[] nums = new int[60]; nums[0] = 2; int count = 1; while(true){ if(count == 50) break; else{ int base = nums[count - 1]; while(true){ base++; if(isPrime(base,nums,count)){ nums[count] = base; count++; break; } } } } for(int i = 0;i < count;i++) System.out.print(nums[i] + " "); } public static boolean isPrime(int num,int[] num2,int count){ boolean re = true; for(int i = 0;i < count;i++){ if(num2[i] > Math.sqrt(num)) break; if(num % num2[i] == 0){ re = false; break; } } return re; } }
-
结果显示:
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 Process finished with exit code 0