Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're including 1 and the <code>candidate</code> in your <code>candidate%divisor</code> tests, both of which will always return 0, so every number you test will appear prime.</p> <p>Instead of this:</p> <pre><code>for(divisor=1;divisor&lt;=candidate;divisor++) </code></pre> <p>do this:</p> <pre><code>for(divisor=2;divisor&lt;candidate;divisor++) </code></pre> <h3>update</h3> <p>There are too many things wrong with your code to list, but here are a couple anyways:</p> <ul> <li>you're checking the result of <code>candidate%divisor</code> outside the for loop</li> <li>by the time you check <code>k</code>, <code>k</code> is always <code>candidate%candidate</code>, or 0</li> <li>you only ever check <code>k</code> once, after the divisor loop</li> <li>don't <code>continue</code> at the end of your loop, that's what happens by default</li> </ul> <p>Look, here is some pseudo-code for the simplest possible implementation. Try to find where your code deviates from this:</p> <pre><code>for candidate = down to up // assume the candidate is primt prime = true // check all divisors from 2 to the candidate - 1, inclusive for divisor = 2 to candidate - 1 if candidate % divisor == 0 // divisor is a factor of candidate // candidate isn't prime, so we can stop checking prime = false break end if next divisor // if prime is still true, we successfully tested every number from 2..candidate // and found no factors if prime print "candidate {candidate} is prime!" end if next candidate </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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