title: Implementation of Fermat's primality test
body: <p>Who wants to help me with my homework?</p>
  
  <p>I'm try to implement <a href="http://en.wikipedia.org/wiki/Fermat_primality_test#Algorithm_and_running_time" rel="nofollow">Fermat's primality test</a> in Java using BigIntegers. My implementation is as follows, but unfortunately it doesn't work.  Any ideas?</p>
  
  <pre><code>public static boolean checkPrime(BigInteger n, int maxIterations)
  {
      if (n.equals(BigInteger.ONE))
          return false;
  
      BigInteger a;
      Random rand = new Random();
  
      for (int i = 0; i &lt; maxIterations; i++)
      {
          a = new BigInteger(n.bitLength() - 1, rand);
          a = a.modPow(n.subtract(BigInteger.ONE), n);
  
          if (!a.equals(BigInteger.ONE))
              return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>I'm new to BigIntegers.</p>
  
  <p>Thanks!</p>