user3635683
user3635683

Reputation:

I do not understand the logic behind this prime number checker (Java)

I do not understand the logic behind this number checker and I'm wondering if somebody could help me understand it a little bit better.

Here's the code:

I will do my best to comment on what's happening but I do not fully understand it.

//find prime numbers between 2 and 100

class PrimeNumberFinder {
    public static void main(String args[]) {

        int i, j; // declare the integer variables "i" and "j"
        boolean isPrime; // declare the Boolean variable is prime but do not assign value

        // create a for loop that starts at two and stops at 99.
        for (i=2; i < 100 ; i++) {
            isPrime = true; // I do not know why isPrime is set to true here.
            // This is where I get confused badly.. we give the "j" variable a value of two and check to see if it's less than whatever "i" divided by "j" is.             
            // If "i=2" then how would j (which is = 2) be less than or equal to i/j (2/2)? 

            for (j = 2; j <= i/j; j++)
                if ((i%j) == 0) isPrime = false; // If a certain number goes in evenly that isn't 1, or "i" itself, it isn't prime so we set the boolean to false

            if (isPrime) // if true print i
                System.out.println(i + " Is a prime number");


        }
    }
}

As you can see the second for loop and almost everything going on within it confuses me, especially the "j <= i/j" because to me j is always going to be bigger.. and why is "j" even increasing? Can't you just divide it by two and determine whether or not it's prime that way?

Any help is greatly appreciated, thank you for reading.

Upvotes: 1

Views: 1656

Answers (1)

Jyr
Jyr

Reputation: 711

Let's go through it line by line.

int i, j;
boolean isPrime;

We start with declaring our variables. Nothing too fancy.

for (i=2; i < 100; i++) {
    isPrime = true;

Here we enter our loop that basically contains all the number we are going to check (here: 2 - 99). We also state that the current number is a prime number (unless proven otherwise).

    for (j = 2; j <= i/j; j++)
        if ((i%j) == 0) isPrime = false;

Now here is where the magic happens. We are going to check if we can divide the current number i evenly by any integer ranging from j == 2 to i/j (i/j ultimately is just a fancy way of writing Math.sqrt(i)). So why up until there?

Well, say we have two divisors a and b such that a * b = i. Now, if divisor a is bigger than the square root of i, then the other divisor b will be smaller than the square root of i. If not then a * b > i and this isn't possible.

So, if we can find a case in which we can divide evenly, this explicitly means that the current number is not prime and we set the isPrime variable to false.

    if (isPrime) // if true print i
        System.out.println(i + " Is a prime number");

}

So, if we still have isPrime == true, it means that the current number withstood our test and we can print it.

Two further improvements;

  1. Once we know the number is not prime, there is no need to check any additional divisors, so we want to exit the loop and hence a break; statement could be addded.
  2. 2 is the only even prime number, so alternatively you could start the second loop at j == 3 and increase by 2 after every execution. You'll then have to consider the case when i == 2 separately.

Upvotes: 1

Related Questions