简体   繁体   English

使用递归方法识别素数 [java]

[英]identify prime number using recursive method [java]

the problem is below.问题在下面。 main() checks numbers 1-10 by calling isPrime(). main() 通过调用 isPrime() 检查数字 1-10。 I think I have the math right however every number other than 2 comes back as not prime.我想我的数学是对的,但是除了 2 以外的每个数字都不是素数。

I have checked some of the solutions and questions on SO, however, I can't seem to achieve the same results.我已经检查了一些关于 SO 的解决方案和问题,但是,我似乎无法获得相同的结果。

original problem:原问题:

public class PrimeChecker {
// Returns 0 if value is not prime, 1 if value is prime
   public static int isPrime(int testVal, int divVal) {
      // Base case 1: 0 and 1 are not prime, testVal is not prime

      // Base case 2: testVal only divisible by 1, testVal is prime

      // Recursive Case
         // Check if testVal can be evenly divided by divVal
         // Hint: use the % operator

         // If not, recursive call to isPrime with testVal and (divVal - 1)
      return 0;
   }

   public static void main(String[] args) {
      int primeCheckVal = 0; // Value checked for prime

      // Check primes for values 1 to 10
      for (primeCheckVal = 1; primeCheckVal <= 10; ++primeCheckVal) {
         if (isPrime(primeCheckVal, (primeCheckVal - 1)) == 1) {
            System.out.println(primeCheckVal + " is prime.");
         }
         else {
            System.out.println(primeCheckVal + " is not prime.");
         }
      }
   }
}

My solution so far:到目前为止我的解决方案:

     public class PrimeChecker {
     // Returns 0 if value is not prime, 1 if value is prime
   public static int isPrime(int testVal, int divVal) {
      int resultVal = 0;

      if ((testVal == 0) || (testVal == 1)){
         resultVal = 0;
      }// Base case 1: 0 and 1 are not prime, testVal is not prime

      else if (divVal == 1) {
         resultVal = 1;
      }// Base case 2: testVal only divisible by 1, testVal is prime

      else {
         if((testVal % divVal != 0) && (testVal / divVal == 1)) {
            isPrime(testVal, (divVal-1));
         }
         else {            
             resultVal = 1;

      }
      }
      return resultVal;
       }

      public static void main(String[] args) {
      int primeCheckVal = 0; // Value checked for prime

      // Check primes for values 1 to 10
      for (primeCheckVal = 1; primeCheckVal <= 10; ++primeCheckVal) {
        if (isPrime(primeCheckVal, (primeCheckVal - 1)) == 1) {
            System.out.println(primeCheckVal + " is prime.");
         }
         else {
            System.out.println(primeCheckVal + " is not prime.");
         }
      }
   }
}

Change the if/else block 更改if / else块

     if((testVal % divVal != 0) && (testVal / divVal == 1)) {
        isPrime(testVal, (divVal-1));
     }
     else {            
         resultVal = 1;

  }

to

if (testVal % divVal != 0) {
  return isPrime(testVal, (divVal-1));
} else {            
  resultVal = 0;
}

Basically, you've forgotten to return the result of your recursion, so the code carries on to return the wrong thing. 基本上,您忘记了返回递归的结果,因此代码继续执行以返回错误的内容。 If testVal % divVal == 0 , the number is non-prime so you return zero. 如果testVal % divVal == 0 ,则该数字为非素数,因此您返回零。 Also, don't use ints that only take the value of zero or one; 另外,不要使用仅取零或一的整数; use a boolean . 使用boolean

According your question, I think the following code would be easier.根据你的问题,我认为下面的代码会更容易。


public class PrimeChecker {
// Returns 0 if value is not prime, 1 if value is prime
   public static int isPrime(int testVal, int divVal) {
      // Base case 1: 0 and 1 are not prime, testVal is not prime
      if (testVal <= 1) {
         return 0;
      }
        
      // Base case 2: testVal only divisible by 1, testVal is prime
      if (divVal == 1) {
         return 1;
      }
        
      // Recursive Case
      // Check if testVal can be evenly divided by divVal
      // Hint: use the % operator
      if (testVal % divVal == 0) {
         return 0;
      }
      // If not, recursive call to isPrime with testVal and (divVal - 1)
      return isPrime(testVal, divVal - 1);
   }

   public static void main(String[] args) {
      int primeCheckVal;     // Value checked for prime

      // Check primes for values 1 to 10
      for (primeCheckVal = 1; primeCheckVal <= 10; ++primeCheckVal) {
         if (isPrime(primeCheckVal, (primeCheckVal - 1)) == 1) {
            System.out.println(primeCheckVal + " is prime.");
         }
         else {
            System.out.println(primeCheckVal + " is not prime.");
         }
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM