简体   繁体   中英

Finding a prime number using recursion in Java

I'm writing this problem for school and I have some issues with it. I can't get it to actually calculate the prime number. Obviously, when I run a test from main with the number 4 it says 4 is a prime number when we all know it's not. How do I need to write out the equation?

The instructions are as follows.

Use RECURSION to write this function (or make this function a wrapper over another recursive function)

 * a number is prime if it is divisible only by itself and 1 
 * (that is, if it is not divisible by any number between * itself and 1; 
 * we can optimize and just check between 2 and the square root of the number).
 * by convention, 1 is NOT prime
 * this function returns true if its parameter is prime, false otherwise.
 * One way to do this is to test all numbers between 2 and n; if any of them     
 * divides it, then it is not prime. If you reach the end, then it is.
 * Examples:
 * isPrime(1) => false
 * isPrime(2) => true
 * isPrime(3) => true
 * isPrime(4) => false
 */



public static boolean isPrime(int n)
{
    if (n == 0 || n == 1) { 
        return false; 
    } if (n == 2 || n == 3) { 
        return true; 
    } if (Math.sqrt(n) % 2 == 0) { 
        return true;
    }else
        return isPrime(n);
    }

The below code is from the grader.java that my prof uses to grade the program. There are a few calls to the isprime method. It always seems to get hung up on 4 (I see why... 4 squared % 2 == 0) and 4 isn't a prime #.

         public void testIsPrime()
{
    Assert.assertEquals("1 is not prime", false,Assignment4.isPrime(1));
    Assert.assertEquals("2 is prime", true,Assignment4.isPrime(3));
    Assert.assertEquals("4 is not prime", false,Assignment4.isPrime(4));
    Assert.assertEquals("7 is prime", true,Assignment4.isPrime(7));
    Assert.assertEquals("9 is not prime", false,Assignment4.isPrime(9));
    Assert.assertEquals("35 is not prime", false,Assignment4.isPrime(35));
    Assert.assertEquals("37 is prime", true,Assignment4.isPrime(37));        
}

The assignment is giving you a vital hint:

or make this function a wrapper over another recursive function

public static boolean isPrime_helper(int number, int divisor)
{
    /* write code to return true if divisor is > square root of number */
    /* which can also be expressed divisor squared is > number */

    /* write code here to test if divisor divides number without remainder */
    /* and return false if it does.  Otherwise: */

    return isPrime_helper(number, divisor + 2);
}

public static boolean isPrime(int number)
{
    /* deal with the special cases 2, < 2, and even numbers here */
    /* Otherwise: */

    return isPrime_helper(number, 3);
}

cdlane 's answer has the basic correction. I just want to make sure that you know where your try doesn't work. You have two fatal problems:

  1. You have no simplification in your recursion. If you haven't hit the base cases (0-3), you recur with the same number, throwing you into an infinite loop.
  2. Your sqrt clause is both needless and wrong. If the sqrt of a number is even, it cannot be prime. Is this supposed to be a test for stopping the recursion?

In Prime numbers, 2 can't be prime because you can divide it by 2 and it shouldn't be able to divide by 2. Hence, if number%2 is 0 or number is 2 you need to return false.

When a number is not 2 or it's not possible to divide it by 0, you can check for other numbers with a for loop inside your function.

Take a look at following code it helps you to understand what's going on:

public boolean isPrime (int number){
    if ((number%2)==0 && number != 2) {
        return false;
    }
    else {
        for (int i =3; i*i<number; i++ )
        {
            if (number%i ==0)
                return false;
        }
        return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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