Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem with your code is that you just check one of the prime factors, not all of them.</p> <p>Take your example of 14. Your code only checks if 2,3 or 5 is a factor of 14, which is not exactly what you need. Indeed, you find that 2 is a factor of 14, but the other factor is 7, as you said. What you are missing is to further check if 7 has as only factors 2,3 and 5 (which is not the case). What you need to do is to eliminate all the factors 2,3 and 5 and see what is remaining.</p> <p>Let's take two examples: 60 and 42</p> <p><strong>For 60</strong></p> <p>Start with factors 2</p> <ul> <li>60 % 2 = 0, so now check 60 / 2 = 30.</li> <li>30 % 2 = 0, so now check 30 / 2 = 15.</li> <li>15 % 2 = 1, so no more factors of 2.</li> </ul> <p>Go on with factors 3</p> <ul> <li>15 % 3 = 0, so now check 15 / 3 = 5.</li> <li>5 % 3 = 2, so no more factors of 3.</li> </ul> <p>Finish with factors 5</p> <ul> <li>5 % 5 = 0, so now check 5 / 5 = 1</li> <li>1 % 5 = 1, so no more factors of 5.</li> </ul> <p>We end up with 1, so this number is part of the sequence.</p> <p><strong>For 42</strong></p> <p>Again, start with factors 2</p> <ul> <li>42 % 2 = 0, so now check 42 / 2 = 21.</li> <li>21 % 2 = 1, so no more factors of 2.</li> </ul> <p>Go on with factors 3</p> <ul> <li>21 % 3 = 0, so now check 21 / 3 = 7.</li> <li>7 % 3 = 1, so no more factors of 3.</li> </ul> <p>Finish with factors 5</p> <ul> <li>7 % 5 = 2, so no more factors of 5.</li> </ul> <p>We end up with 7 (something different from 1), so this number is NOT part of the sequence.</p> <p>So in your implementation, you should probably nest 3 while loops in your for loop to reflect this reasoning.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload