Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's start with the first four lines of the function's code:</p> <pre><code>def isprime(n): if n == 2: return True if n == 3: return True if n % 2 == 0: return False if n % 3 == 0: return False </code></pre> <p>The function tests to see if <code>n</code> is equal to 2 or 3 first. Since they are both prime numbers, the function will return <code>True</code> if <code>n</code> is equal to either.</p> <p>Next, the function tests to see if <code>n</code> is divisible by 2 or 3 and returning <code>False</code> if either is true. This eliminates an extremely large amount of cases because half of all numbers above two are not primes - they are divisible by 2. The same reason applies to testing for divisibility by 3 - it also eliminates a large number of cases.</p> <p>The trickier part of the function is in the next few lines:</p> <pre><code>i = 5 w = 2 while i * i &lt;= n: if n % i == 0: return False i += w w = 6 - w return True </code></pre> <p>First, <code>i</code> (or index) is set to 5. 2 and 3 have already been tested, and 4 was tested with <code>n % 2</code>. So, it makes sense to start at 5.</p> <p>Next, <code>w</code> is set to 2. <code>w</code> seems to be an "incrementer". By now, the function has tested for all even numbers (<code>n % 2</code>), so it would be faster to increment by 2.</p> <p>The function enters a <code>while</code> loop with the condition <code>i * i &lt;= n</code>. This test is used because <a href="http://www.millersville.edu/~bikenaga/number-theory/primes/primes.html" rel="nofollow">every composite number has a proper factor less than or equal to its square root</a>. It wouldn't make sense to test numbers after the square root because it would be redundant.</p> <p>In the <code>while</code> loop, if <code>n</code> is divisible by <code>i</code>, then it is not prime and the function returns <code>False</code>. If it is not, <code>i</code> is incremented by the "incrementer" <code>w</code>, which, again, is faster.</p> <p>Perhaps the trickiest part of the function lies in the second-to-last line: <code>w = 6 - w</code>. This causes the "incrementer" <code>w</code> to toggle between the values 2 and 4 with each pass through loop. In cases where <code>w</code> is 4, we are bypassing a number divisible by 3. This is faster than remaining at 2 because the function already tested for divisibility by both 2 and 3.</p> <p>Finally, the function returns <code>True</code>. If the function hasn't detected any cases where <code>n</code> is divisible by something, then it must be a prime number.</p>
    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. 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.
    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