4

I don't understand "return " value at the end of Method isPrime, it has value true .

public class PrimeNumber extends ConsoleProgram{
public void run(){
    int number = readInt("Enter number: ");
    if(isPrime(number)){
        println( number + " is prime number");
    }else{
        println(number + " is not a prime number");
    };
}   


private boolean isPrime(int n){

    for(int i=2;i<n;i++){
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

It returns false when it finds divider, but i don't get why outside the loop it returns true?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 4
    The `isPrime` function assumes the number is prime. The loop is searching for a counter-example that would disprove this assumption. If no counter-example is found, the loop reaches its end and the function returns `true`. – jbabey Jul 11 '13 at 18:56
  • @jbabey A perfect answer - why post as a comment? – Duncan Jones Jul 11 '13 at 19:01
  • @jbabey. Your answer is better than all the posted answer. Please add it as an answer. – Rohit Jain Jul 11 '13 at 19:04

4 Answers4

1

It returns true by default. So if it finishes the loop, going through all the numbers checking for divisibility, and finds that none divide it, it returns true. It is written in a way that your method will return false if it finds any number that divides your parameter, and will return true only if it checks all the numbers it wants to check and finds none dividing the parameter with remainder zero.

A method declared with boolean return type MUST return something always, the method cannot simply end without returning anything.

Note: You don't have to check every single number last than X for divisibilty to see if x is prime. You only have to check up to and including the square root of X, see this for reference: Why do we check up to the square root of a prime number to determine if it is prime?

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
1

The isPrime function assumes the number is prime. The loop is searching for a counter-example that would disprove this assumption. If no counter-example is found, the loop reaches its end and the function returns true.

This is known in logic as a proof by contradiction. In cases where most of your results will be false (such as this, since most numbers are not prime), it is regarded as a good programming practice because it leads to algorithms that fail and return as quickly as possible, resulting in better performance.

jbabey
  • 45,965
  • 12
  • 71
  • 94
0

Basically, this lets the programmer take some liberties in simplification of the code in this case.

If inside the loop, (n % i == 0) evaluates to true, the if statement is taken. false is returned and the method stops.

In the alternative, if that expression is never true, the loop is exited, and true is returned.

Placing a return at the end of a piece of code that can return something conditionally helps with cleaning up an initial condition and avoids compiler warnings or errors.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

he is right, but if you want to check if the number is a prime one, you only have to check to n/2!

mspringsits
  • 43
  • 1
  • 9