Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Someone has reformatted the code for clarity from its original submitted form. In doing so, they've modified one of the three mistakes in the code.</p> <p>The first mistake was that the second for statement did not have brackets after it. As a result, the loop body consisted only of the statement which followed the second for statement. From reading the code, it's clear that the questioner intended everything after the second loop statement to be executed repeatedly. This is not what was happening.</p> <p>Essentially, the idea was: for each number in the range, try all numbers between 1 and the candidate. Count the number of those which divide it. See if there are exactly 2. This is a correct algorithm and the answer which say to not divide by 2 is wrong.</p> <p>However, the implementation was doing this: for each number in range, divide the candidate by every number between 1 and the candidate. Then, once you've done all that division, check to see if the last one came out even and if so, add 1 to the count. So, what's happening is that you're only actually checking to see if <code>candidate % candidate == 0</code> which it always does.</p> <p>The second mistake, is that the count is only reset when it gets to 2. So, what's happening is that the code, for any input, will return every other number. For each candidate, <code>count++</code> happens exactly once. So it reaches 2 and gets reset every other time.</p> <p>The third mistake is that once the inner-most loop is fixed, then the continue will do the wrong thing since it only continues the inner-most loop.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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