Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Factor an integer using Fermat's factorization algorithm
ResourceFunction["FactorIntegerFermat"][n] factors the integer n using Fermat's algorithm. |
A trivial factorization for Fermat's algorithm:
In[1]:= |
Out[1]= |
A slightly larger example:
In[2]:= |
Out[2]= |
In the following example, we see that the factor returned by FactorIntegerFermat is close to :
In[3]:= |
Out[4]= |
The ratio of size difference between the square root and a factor to the size of the square root is around 1/2, which means the factor is removed from the square root of bign by approximately its fourth root.
In[5]:= |
Out[6]= |
In[7]:= |
Out[7]= |
In[8]:= |
Out[8]= |
FactorIntegerFermat scales well to larger semiprimes, providing the two primes are close to :
In[9]:= |
Out[12]= |
Factoring the following semiprime requires 262143 iterations, which is more than the default number of iterations (216):
In[13]:= |
Out[13]= |
Increasing MaxIterations obtains the factorization:
In[14]:= |
Out[14]= |
Lehman extended Fermat's algorithm to factor N=p q using a rational approximation to the ratio of the factors, p/q:
In[15]:= |
Out[15]= |
In[16]:= |
Out[18]= |
This works as 10277/1199 is a good approximation to p/q:
In[19]:= |
Out[19]= |
Unlike FactorInteger, FactorIntegerFermat is not a general purpose factoring algorithm:
In[20]:= |
Out[20]= |
In[21]:= |
Out[21]= |
In[22]:= |
Out[22]= |
FactorInteger succeeds:
In[23]:= |
Out[23]= |
FactorIntegerFermat does not handle this case:
In[24]:= |
Out[24]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License