# Question about Prime number exercise

**URL:** <https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235>\
**Category:** JavaScript\
**Created:** [June 22, 2022, 4:04pm UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235 "2022-06-22T16:04:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wly21](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wly21](https://forum.codewithmosh.com/u/wly21)\
**Post date:** [June 22, 2022, 4:04pm UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/1 "2022-06-22T16:04:44Z")

</div>

In the Javascript part 1 course, the last exercise of control flow is show all the prime numbers within the limit.

The script is:

> function showPrimes(limit){  
> for (let number=2; number \<= limit; number++{  
> let isPrime = true;  
> for (let factor = 2; factor \< number; factor++){  
> if (number % factor === 0){  
> isPrime = false;  
> break;  
> }  
> }  
> if (isPrime) console.log(number);  
> }  
> }

What I can’t figure out is this part:  
if (number % factor === 0)

I’m thinking that factor = 2, and 2 \< 3, so factor +1. Iterate it, now all the factors are 2-10.

The next is after number/factor, if there is no remainder, it is not a prime number.  
So 3(number)/2(factor), there is a remainder so 3 is a prime number.  
The next is what confuse me.  
Both number and factor increment by 1, so now 4/3, this also has a remainder, so according to the code, 4 is also a prime number.

What am I thinking wrongly here?

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [June 23, 2022, 3:00am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/2 "2022-06-23T03:00:41Z")

</div>

According to the code, 4 is not a prime number. You can run the code to confirm.

When `number` is `4` and `factor` is `2`, then `number % factor === 0` and `isPrime` is set to `false` and we `break` out of the inner loop and move on to the next number.

`number` and `factor` are not incremented at the same time. Maybe some extra logging will help?

```javascript
function showPrimes(limit) {
  for (let number=2; number <= limit; number++) {
    console.log("number is " + number);
    let isPrime = true;
    for (let factor = 2; factor < number; factor++) {
      console.log("factor is " + factor);
      if (number % factor === 0) {
        isPrime = false;
        console.log("not a prime");
        break;
      }
    }
    if (isPrime) {
      console.log(number);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [June 23, 2022, 4:05am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/3 "2022-06-23T04:05:35Z")

</div>

> [@wly21](#):
>
> So 3(number)/2(factor), there is a remainder so 3 is a prime number.  
> The next is what confuse me.  
> Both number and factor increment by 1, so now 4/3…

I think the part that is confusing you is that the inner loop runs to completion before the outer loop increments.

For example, if I have the following code:

```javascript
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    console.log("i =", i, ", j =", j);
  }
}

```

It will print:

```auto
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2

```

See how the outer for loop only increments after the inner for loop has completed. If we use a method for the inner for loop, the logic becomes even clearer:

```auto
function showPrimes(limit) {
  for (let number = 2; number <= limit; number++) {
    if (isPrime(number)) console.log(number);
  }
}

function isPrime(number) {
  for (let factor = 2; factor < number; factor++) {
    if (number % factor === 0) return false;
  }
  return true;
}

```

Aside: this is one of the reasons breaking things down into functions can greatly improve the readability of your code.

So, even though each for loop increments by 1, they are doing so at different times. Extracting a function here helps with the readability.

Lastly, this is probably the least efficient way to determine the primes up to a given number and would be something I would call a “brute force” method. If you ever need to do this in an interview, please use the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).

---

<div class="post-metadata">

**Author:** ![wly21](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wly21](https://forum.codewithmosh.com/u/wly21)\
**Post date:** [June 23, 2022, 4:31am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/4 "2022-06-23T04:31:59Z")

</div>

Hi eelsholz, thanks for the explanation! This is very helpful!  
Now I know that the outer loop and inner loop does increment at the same time.

---

<div class="post-metadata">

**Author:** ![wly21](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wly21](https://forum.codewithmosh.com/u/wly21)\
**Post date:** [June 23, 2022, 4:33am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/5 "2022-06-23T04:33:21Z")

</div>

Hi jmrunkle, your explanation is awesome!  
Also thanks for the additional info about Sieve of Eratosthenes.  
I will read about it.

---

<div class="post-metadata">

**Author:** ![DiegoHerrera91](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DiegoHerrera91](https://forum.codewithmosh.com/u/DiegoHerrera91)\
**Post date:** [October 26, 2022, 6:24am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/6 "2022-10-26T06:24:17Z")

</div>

I´ve got a question too, in the first iteration of both for loops, when number = 2 in the outer for loop, and factor \< number (which means less than 2) in the inner for loop, what would be the value of the factor then? so then within the if condition, the division doesn’t have a remainder = 0

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [October 27, 2022, 2:07am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/7 "2022-10-27T02:07:16Z")

</div>

When the outer loop variable `number` is 2, the inner loop is never really entered because the initialization sets the factor to 2 and then the condition is checked before the body executes. Since the condition is `factor < number` and `2<2` returns false we never execute the body of the for loop. Therefore we skip over that and return true. It is easier to reason about if you use my factored version where we have a helper method called `isPrime`.

---

<div class="post-metadata">

**Author:** ![sabo](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sabo](https://forum.codewithmosh.com/u/sabo)\
**Post date:** [August 23, 2023, 12:23am UTC](https://forum.codewithmosh.com/t/question-about-prime-number-exercise/13235/8 "2023-08-23T00:23:39Z")

</div>

A little over a year later and wow this helped me understand it so clearly. Thanks very much! Wasn’t truly grasping the inner loop until adding the console log.
