title: Explain a code to check primality based on Fermat's little theorem
body: <p>I found some Python code that claims checking primality based on <a href="https://en.wikipedia.org/wiki/Fermat%27s_little_theorem" rel="nofollow noreferrer">Fermat's little theorem</a>:</p>
  
  <pre><code>def CheckIfProbablyPrime(x):
      return (2 &lt;&lt; x - 2) % x == 1
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>How does it work?</li>
  <li>What's its relation to Fermat's little theorem?</li>
  <li>How accurate is this method? </li>
  <li>If it's not accurate, what's the advantage of using it?</li>
  </ol>
  
  <p>I found it <a href="https://stackoverflow.com/questions/12421969/finding-all-divisors-of-a-number-optimization">here</a>.</p>