0

Trying to come up with a function to check if a number is prime and I'm running into trouble. I'm sure there's a simpler way to do this, but why would this function not return false, for the number 9? It returns false for even numbers but for any other type of composite number it returns undefined, but since it prints NOT PRIME it should also be returning false.

function isPrime(n, i) {
    document.writeln(i);
    var nextNum = i + 1;
    var number = n;
    if (i < n) {
        if ((n % i) === 0) {
            document.writeln("NOT PRIME");
            return false;
        } else {
            document.writeln(nextNum);
            isPrime(number, nextNum);
        }
    } else if (i === n) {
        document.writeln("Recursion ends");
        return true;
    } else {
        document.writeln("Confused" + typeof i + typeof n);
    }
}
fred02138
  • 3,323
  • 1
  • 14
  • 17
  • How are you calling `isPrime`? Why does it take 2 parameters? – gen_Eric Nov 06 '13 at 18:27
  • `isPrime(testNumber,2);` where testNumber is the number we're testing – user2961738 Nov 06 '13 at 18:30
  • 1
    Ummmm what does the 2 represent? – Naftali Nov 06 '13 at 18:32
  • Already discussed in depth @ http://stackoverflow.com/questions/11966520/how-to-find-prime-numbers – Gal Samuel Nov 06 '13 at 18:34
  • Well, the 2 is the first number to check with modular division because anything % 1 = 0. I'm sure it's kind of a dumb way of doing this but the second argument is for the purpose of recursion because I guess it avoids using a global variable? – user2961738 Nov 06 '13 at 18:34
  • @user2961738 see [my answer](http://stackoverflow.com/a/19820048/561731) for a way to avoid passing in a second argument :-) – Naftali Nov 06 '13 at 18:39

3 Answers3

7

You need to return the value of the recursive call, i.e., change

isPrime(number, nextNum);

to

return isPrime(number, nextNum);
fred02138
  • 3,323
  • 1
  • 14
  • 17
1

You are missing a return in this branch after the recursive call to isPrime:

    if ((n % i) === 0) {
        document.writeln("NOT PRIME");
        return false;
    } else {
        document.writeln(nextNum);
        isPrime(number, nextNum);
    }

I think that you want to change it to:

    if ((n % i) === 0) {
        document.writeln("NOT PRIME");
        return false;
    } else {
        document.writeln(nextNum);
        return isPrime(number, nextNum);
    }

Because you aren't returning anything in that branch, the true/false calls are disappearing.

Schleis
  • 41,516
  • 7
  • 68
  • 87
1

It should just need one parameter to check if prime.

Try this out:

function isPrime(num){

    // An integer is prime if it is not divisible by any prime less than or equal to its square root
    var squareRoot = parseInt(Math.sqrt(num));
    var primeCountUp = function(divisor){
        if(divisor > squareRoot) {
            // got to a point where the divisor is greater than 
            // the square root, therefore it is prime
            return true;
        }
        else if(num % divisor === 0) {
            // found a result that divides evenly, NOT prime
            return false;
        }
        else {
            // keep counting
            return primeCountUp(++divisor);
        }
    };

    // start @ 2 because everything is divisible by 1
    return primeCountUp(2);

}

Adding the high of the "square root" from here

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303