[英]Writing a method to find prime numbers
我正在嘗試編寫一個程序,該程序使用謂詞方法來查找1-100之間的所有素數。 我知道找到質數有更有效的方法,但是現在,但是我想使用蠻力策略並嘗試所有可能的組合。
現在,該程序原樣只打印10,000次對或錯,但是我希望我的程序只打印素數的數字。 因此,程序完成后,我將得到一個介於1至100之間的質數列表。
1.我的程序對我要執行的操作是否正確? 2.建議更改我的程序,以便它列出1至100之間的所有素數。
import acm.program.*;
public class PrimeNumbers extends ConsoleProgram{
public void run(){
for (int i =1; i <= 100, i++){
for (int j =1; j<= 100; j++){
println(yesPrime(i, j));
}
}
}
private boolean yesPrime (int n, int k){
return ( n % k == 0)
}
}
}
您沒有檢查素數。 您正在測試從10,000到10,000的兩個數字的所有組合,以查看第二個數字是否均等地除以第一個數字。
但這可能正確地做到了。
您想要做什么的偽代碼:
for each number n from 2:100
for each divisor d from 2:n-1
test to see if d divided n evenly
end for
if no values of d other than n divided n evenly
print "n is prime"
end for
有幾個優化方案可供您考慮:
玩得開心!
使用Eratosthenes篩子:
public static void main(String[] args) {
int n = 100; // the max number for test
// Sieve of Eratosthenes
boolean[] sieve = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
sieve[i] = true;
}
for (int i = 2; i <= n; i++) {
if (sieve[i] != false) {
for (int j = i; j * i <= n; j++) {
sieve[i * j] = false;
}
}
}
// Print prime numbers
for (int i = 0; i <= n; i++) {
if (sieve[i]) {
System.out.println(i);
}
}
}
好吧,您要從yesPrime
返回比較結果,然后在run
打印該比較結果。 猜猜輸出是什么。
考慮到這是一項任務,我想給你一個提示而不是答案。
檢查yesPrime
的結果。 如果為true,則打印數字並退出循環。
Scanner reader = new Scanner(System.in);
System.out.println("Enter the a number");
int num = reader.nextInt();
int counter = 0;
int root = 0;
boolean prime_flag;
if (2 <= num) {
// 2 is the only even prime number
counter++;
}
for (int i = 3; i < (num + 1); i++) {
// test only for odd number
if (i % 2 != 0) {
prime_flag = true;
root = (int) (Math.sqrt(i) + 1);
for (int j = 3; j < (root + 1); j++) {
if ((i % j == 0) && (i != j)) {
prime_flag = false;
break;
}
}
if (prime_flag) {
counter++;
}
}
}
System.out.println("Count of prime numbers upto " + num + " is "
+ counter);
我將創建一個函數,類似於您的yesPrime
函數。 這僅需要一個數字,並檢查該數字是否為質數。
就像是
boolean yesPrime(int n)
{
// check to see if n is divisble by numbers between 2 and sqrt(n)
// if the number is divisible, it is not prime, so return false
// after the loop has checked all numbers up to sqrt(n), n must be prime so return true
}
然后在主程序中,將數字1循環到100,並為每個數字調用yesPrime。 如果結果為真,則打印該號碼。
我的理解是編程的一個目標是將問題分解為較小的子問題。 通過編寫用於測試質數的函數,可以避免在一個函數中使用嵌套循環,而這可能很難理解。
對於初學者,您需要檢查從2開始的質數。不要對所有100個數字進行質數檢查,僅將每個數字作為從2到(number-1)的因子進行檢查。每個數字都可以被1整除。
public static void main(String[] args) {
boolean b;
for (int i = 2; i < 100; i++) {
b = checkPrime(i);
if (b)
System.out.println(i);
}
}
private static boolean checkPrime(int k) {
for (int i = 2; i < k; i++) {
//check if the number is divisible by any number starting from 2 till number -1.If it is, it is not a prime number
if (k % i == 0)
return false;
}
// return true if the number is not divisible by any number from 2 to number -1 i.e. it s a prime number.
return true;
}
想想這個邏輯
所有可被2整除的數字不是素數,因此您可以將數字加2
如果一個數字不能被質數整除,那么它就是質數
試試這個代碼
public static void main(String[] args) throws IOException
{
int[] prime=new int[50]; //array to store prime numbers| within 1 to ==> prime numbers will be <=n/2 here n=100
int i=1; //index for "num" array
int j=1; //index for storing to "prime" array
int k=1; //index for browsing through prime array
prime[0]=2; // setting the first element
int flag=1; // to check if a number is divisibe for than 2 times
for(i=3;i<=100;i+=2) {
for(k=0;prime[k]!=0;k++) //browsing through the array to till non zero number is encountered
{
if(i%prime[k]==0) flag++; //checking if the number is divisible, if so the flag is incremented
}
if(flag<2)
{
prime[j++]=i; // if the flag is still 1 then the number is a prime number
}
flag=1;
}
System.out.println(Arrays.toString(prime)); //a short way to print an array
}
查找給定范圍內的質數很容易/ *請實現此方法以返回給定范圍(含)內所有質數的列表。 質數是一個自然數,正好具有兩個不同的自然數除數,即1和素數本身。 第一個質數是:2、3、5、7、11、13 * /
public void getPrimeNumbers(int st, int en){
// int st=1, en=100;
for(int i=st;i<=en;i++)
if( (i%2!=0) && (i%1==0 && i%i==0) )
System.out.println("Yes prime");
}
public static void main(String[] args) {
boolean isPrime = true; //set isPrime to true
for (int i = 2; i<100;i++){ // consider i is increasing number
for (int j=2; j<i; j++) //j is divisor & must be less than
//i and increasing after each iteration
{
if((i % j)== 0){ // if i gets divisible by
// any increasing value of j from 2
//then enters in this case and makes value
//nonPrime
isPrime=false; //changes value
break; //breaks "for loop of j"
}
} //ends "for loop of j"
if(isPrime) // if case wont work then number is prime
{
System.out.println(i + " is Prime");
}
isPrime = true; //sets isPrime to default true.
}
}
使用不太復雜的方法,我們可以執行以下操作:
import java.math.BigInteger;
public class PrimeNumbers {
public static void main(String[] args) {
BigInteger min = new BigInteger("2");
BigInteger max = new BigInteger("100");
while(min.compareTo(max) < 0){
System.out.println(min);
min = min.nextProbablePrime();
}
}
}
這是我尋找素數的解決方案。
public class PrimeNumberFinder {
public boolean isPrime(int number) {
return number > 1 && IntStream.rangeClosed(2, number / 2)
.noneMatch(value -> number % value == 0);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.