Some bigger numbers

In spite of its name, Fermat's Little Theorem is very powerful.  With it we can identify that certain large integers are composite without actually finding a factorization.  For example, is m = 8453305711931797 a prime?

m=8453305711931797;
PowerMod[5,m-1,m]//Timing
{ 0.` Second , 3574688171559123 }

The output above tells us just about instantly that 5 m - 1 1 ( mod m ) and so m is composite.  If we insisted on determining whether m is prime by direct factorization we would use a measurable amount time.

FactorInteger[m]//Timing
{ 0.3333333333334849` Second , ( 38421167 1 220016891 1 ) }

To determine whether an integer is prime, Mathematica uses a test based on Fermat's theorem as a start and if the test is inconclusive it preceeds to more sophisticated tests.  Here's a much bigger number - 101 digits long - and we can see that it must be composite too.

m=10^100 + 37;
PowerMod[5,m-1,m]//Timing
{ 0.316666666667515528` Second , 3122149475522081847743323262672804359577315010404033756781433141668608109547190831705225144726923669 }
The converse of Fermat's Little Theorem is False - another example


Converted by Mathematica      April 24, 2000