Fact-checked by Grok 2 weeks ago

Fermat's factorization method

Fermat's factorization method is an for factoring an composite n by expressing it as a , n = x^2 - y^2 = (x - y)(x + y), where x and y are positive integers with x > y > 0, allowing the factors x - y and x + y to be identified once such x and y are found. This approach exploits the identity for differences of squares and is particularly suited to numbers whose prime factors are of similar magnitude, differing by a relatively small even amount. The method was devised by the French mathematician in 1643, as part of his broader contributions to , including work on Mersenne primes and correspondence with contemporaries like Bernard Frénicle de Bessy. Fermat described the technique in a letter, recognizing its potential for efficiently factoring certain composites based on their form, though he did not publish it formally during his lifetime. Over centuries, the method has been rediscovered and refined due to its simplicity, serving as a foundational idea in . In practice, the algorithm begins by computing x_0 = \lceil \sqrt{n} \rceil, the smallest integer greater than or equal to the square root of n. It then iteratively tests values x = x_0 + k for k = 0, 1, 2, \dots, checking whether x^2 - n is a perfect square y^2; the process halts when such a y is found, yielding the factors x - y and x + y. If the factors p and q (with p < q) satisfy q - p = 2y and (p + q)/2 = x, the search requires approximately (q - p)^2 / (8\sqrt{n}) iterations, making it deterministic but potentially slow for large differences. Fermat's method is most efficient when the prime factors are close to each other—ideally within about \sqrt{{grok:render&&&type=render_inline_citation&&&citation_id=4&&&citation_type=wikipedia}}{n} of \sqrt{n}—as this minimizes the number of trials needed, often outperforming trial division in such cases. For instance, it has been used to factor large semiprimes like 2251644881930449333 in just a few steps when factors differ by about 196,000. However, it becomes impractical for numbers with widely separated factors, where more advanced methods like the quadratic sieve or general number field sieve are preferred. Modern variants, such as those incorporating continued fractions by Lehmer and Powers in 1931 or improvements by Morrison and Brillhart in 1975, build on Fermat's core idea to enhance performance for cryptographic applications.

Introduction

Historical Background

Pierre de Fermat first described his factorization method in a letter dated April 7, 1643, to , in response to a challenge to determine whether the number 100895598169 is prime or composite. Fermat replied that it is composite, providing the factorization 100895598169 = 112303 × 898423 using the method, which relies on expressing the number as a difference of squares when its prime factors are close in value. In the context of 17th-century number theory, Fermat was a pioneering figure whose work laid foundational elements for modern analytic number theory, particularly through his investigations into Diophantine equations and properties of primes. His correspondence with contemporaries like Mersenne and Frénicle de Bessy often featured challenges involving integer factorization and solutions to equations such as x^2 + y^4 = z^4, where he employed techniques of infinite descent to establish non-existence of solutions. The factorization method emerged as part of this broader exploration, highlighting Fermat's insight into the structure of composite numbers with proximate factors. During the 18th century, Leonhard Euler adopted and refined Fermat's method in his own number-theoretic studies, applying it to factor specific forms of integers more efficiently, such as those amenable to representation as sums or differences of squares. Euler's enhancements, detailed in works like his observations on , demonstrated the method's utility for practical computations of the era, including factoring numbers that resisted . The method gained recognition for its elegance in cases where prime factors are nearly equal but was noted as inefficient for widely separated factors, limiting its general applicability without computational aids. It saw no substantial modifications until the advent of electronic computers in the 20th century, which enabled systematic searches for suitable squares, reviving interest in its optimized variants for cryptographic challenges.

Mathematical Foundation

Fermat's factorization method relies on the fundamental identity from number theory that any odd positive integer n can be expressed as the difference of two squares if it factors as n = pq, where p and q are odd primes with p \leq q. Specifically, n = a^2 - b^2 = (a - b)(a + b), where a = \frac{p + q}{2} and b = \frac{q - p}{2}. This representation holds because p and q being odd ensures that p + q and q - p are even, making a and b integers. The method presupposes that n is an odd composite number, as even composites are trivially factored by dividing out factors of 2. It is most effective when n is a semiprime (product of exactly two primes) and the factors p and q are close in value, meaning |p - q|\ is small relative to \(\sqrt{n}. In such cases, b is small, allowing a to be found near \sqrt{n}. If n has more than two prime factors or the factors are unbalanced, the method becomes inefficient. To derive the factorization, begin with the equation n = \left( \frac{p + q}{2} \right)^2 - \left( \frac{q - p}{2} \right)^2. Expanding this yields: n = \frac{(p + q)^2}{4} - \frac{(q - p)^2}{4} = \frac{(p + q)^2 - (q - p)^2}{4} = \frac{4pq}{4} = pq, confirming the identity under the parity conditions. The algorithm searches for such a and b by starting with a_0 = \lceil \sqrt{n} \rceil and incrementing a sequentially, checking if a^2 - n is a perfect square b^2. Since a > \sqrt{n}, a^2 - n > 0, and the process terminates when b^2 = a^2 - n is found, yielding factors a - b and a + b. The number of steps required is approximately \frac{(q - p)^2}{8 \sqrt{n}}, which is minimal when the factors are proximate.

Basic Method

Algorithm Description

Fermat's factorization method is a for factoring an odd composite n > 2 by representing it as the , n = a^2 - b^2 = (a - b)(a + b), where a > b > 0 are integers. The algorithm begins by computing the of the of n, denoted s = \lceil \sqrt{n} \rceil, and initializing a = s. This starting point ensures that a^2 \geq n, so d = a^2 - n \geq 0. The core of the algorithm is an iterative . For the current a, compute d = a^2 - n. To check if d is a , compute the integer b = \lfloor \sqrt{d} \rfloor and verify whether b^2 = d. If it does, then n factors as (a - b) and (a + b), and the algorithm terminates with these non-trivial factors (provided a - b > 1). If not, increment a by 1 and repeat the process. A optimization can be applied by ensuring the initial a is odd (adjusting if necessary, as n odd implies a and b have the same ) and incrementing a by 2 thereafter, which halves the search space without missing solutions. The loop continues until a suitable b is found. For a composite n with close factors, this occurs quickly; for a prime n, it terminates at a \approx n/2 with trivial factors 1 and n, after roughly O(n) operations in the worst case. In practice, the method applies only to odd n > 2; even numbers should be divided by 2 first, and preprocessing via trial division for small prime factors is recommended to isolate the core .

Pseudocode

The following pseudocode outlines the basic , including the square check via integer :
function fermat_factor(n):
    if n % 2 == 0:
        return [2, n // 2]  // Handle even case
    if n <= 2:
        return [n]
    
    a = ceil(sqrt(n))
    while True:
        d = a * a - n
        b = floor(sqrt(d))
        if b * b == d:
            p = a - b
            q = a + b
            if p > 1:  // Non-trivial factor
                return [p, q]
        a = a + 1
This implementation assumes an efficient square root function and stops upon finding non-trivial factors. For the parity-optimized version, replace the increment with a = a + 2 after setting a to the nearest greater than or equal to \lceil \sqrt{n} \rceil.

Worked Example

To illustrate the application of Fermat's factorization method, consider the n = 5959, which factors as $59 \times 101. The method exploits the fact that if the prime factors p and q (with p < q) are close in value, then n can be expressed as a difference of squares a^2 - b^2 with small a - \lceil \sqrt{n} \rceil. Begin by computing \sqrt{5959} \approx 77.208, so the initial value is a = 78 (the smallest greater than the square root). For each successive a, compute d = a^2 - 5959 and check if d is a b^2. If so, the factors are a - b and a + b. The steps are summarized in the following table:
aa^2d = a^2 - 5959\sqrt{d} (approx.)Perfect square?
78608412511.18No
79624128216.79No
80640044121Yes (b = 21)
Since d = 441 = 21^2, the factors are $80 - 21 = [59](/page/59) and $80 + 21 = [101](/page/101). Verifying: [59](/page/59) \times [101](/page/101) = 5959. This example highlights the method's efficiency when the factors are close (|101 - 59| = 42), requiring only three iterations to converge, as the difference q - p directly influences the number of trials needed.

Performance Analysis

Complexity and Limitations

The time complexity of Fermat's basic factorization method is determined primarily by the number of iterations required to find a representation of n = pq (with primes p < q) as a difference of squares, which is approximately k = \frac{(\sqrt{q} - \sqrt{p})^2}{2}. When the factors are close (i.e., |p - q| is small relative to \sqrt{n}), this simplifies to roughly k \approx \frac{|p - q|^2}{8n}, requiring O(k) iterations, each involving an integer square root computation and verification by squaring; the overall bit complexity is then O(k (\log n)^2). In the worst case, for highly unbalanced factors (e.g., small p and q \approx n/p), k \approx n/(2p), which can reach O(n) operations. For prime n, the method will never find a factorization and may run indefinitely without success; confirming primality requires dedicated primality tests rather than relying on the absence of factors via Fermat's method, though practical implementations limit searches and switch to other methods earlier. The is O(1), as the algorithm uses only a constant number of variables storing integers up to size O(n), relying on simple arithmetic without additional data structures. Key limitations include its inefficiency for large n with unbalanced factors (e.g., p \ll q), where the grows nearly linearly with n, far exceeding the of division in such cases; the basic algorithm also lacks inherent parallelism, as iterations depend sequentially on prior square checks. Additionally, it is vulnerable when n is prime, as it provides no and requires separate verification. In the best case, when factors differ by 2 (products of twin primes), the method succeeds in roughly 1 step or fewer, as k is minimal. However, for typical random semiprimes with balanced but not exceptionally close factors, average remains poor, often requiring many more iterations than optimized alternatives. In modern contexts, the basic method is largely obsolete for general-purpose factorization of large integers due to its sensitivity to factor imbalance, but it retains utility for special cases where |p - q| < n^{1/4}, as the iteration count then bounds to O(n^{1/4}), enabling quick resolution before resorting to more robust techniques.

Comparison with Trial Division

Trial division is a straightforward factorization technique that systematically tests potential divisors of an integer n starting from 2 up to \sqrt{n}, typically in steps of 2 after checking 2 to avoid even numbers, with a worst-case time complexity of O(\sqrt{n}). This method efficiently identifies small prime factors early but becomes computationally expensive for large n with balanced factors, as it may require checking nearly all divisors up to \sqrt{n}. Fermat's method outperforms trial division when the prime factors p and q of n = p \times q are close, i.e., |p - q| \ll \sqrt{n}, because it requires only approximately \frac{|p - q|^2}{8n} iterations to find the difference-of-squares representation, compared to trial division's full O(\sqrt{n}) scans. For instance, in poorly generated moduli where p \approx q, Fermat's approach can factor n rapidly by exploiting this proximity, whereas trial division would exhaustively test up to \sqrt{n} \approx p. Conversely, trial division excels over Fermat's method when the factors are unbalanced, such as one small prime p and a large q \approx n/p, as it discovers the small p after a limited number of tests proportional to p, avoiding the need for extensive searches. In such cases, Fermat's method inefficiently scans from \lceil \sqrt{n} \rceil onward, potentially requiring up to O(\sqrt{n}) steps without early termination. A hybrid strategy mitigates these limitations by first applying trial division to eliminate small factors (e.g., up to a threshold like $10^6) and then using Fermat's method on the cofactor if the remaining factors are suspected to be close. For sufficiently small differences relative to n, such as |p - q| \ll n^{1/4}, Fermat's method requires far fewer operations than trial division's O(\sqrt{n}).
Factor ConfigurationTrial Division ComplexityFermat's Method Complexity
Close factors (p \approx q)O(\sqrt{n})$O\left( \frac{
Distant factors (p \ll q)O(p) (if p small)O(\sqrt{n})

Optimizations and Improvements

Sieving Techniques

Sieving techniques enhance the efficiency of Fermat's factorization method by pre-filtering candidate values of a using , thereby avoiding unnecessary computations for values where a^2 - n cannot be a . The core idea is to choose a small m, often the product of distinct small primes (e.g., m = 3 \times 5 \times 7 = 105), and precompute the set of s modulo m—the possible values that perfect squares can take modulo m. For each candidate a, compute a^2 - n \pmod{m}; if this value is not a modulo m, then a^2 - n cannot be a , and a can be skipped. This approach reduces the proportion of tested a values to roughly the density of s, which is about $1/2 on average for prime but varies with the choice of m. To implement the sieve, first determine the quadratic residues modulo m by squaring all integers from 0 to m-1 and collecting the unique results m. Then, identify the residue classes for a \pmod{m} such that a^2 - n \pmod{m} lands in this set. Only candidates a congruent to these classes m are tested in the main loop starting from a = \lceil \sqrt{n} \rceil + 1. For example, with m = 4, quadratic residues are 0 and 1. If n \equiv 3 \pmod{4}, then for odd a \equiv 1 or $3 \pmod{4}, a^2 \equiv 1 \pmod{4}, so a^2 - n \equiv 1 - 3 \equiv 2 \pmod{4}, which is not a quadratic residue; thus, odd a are skipped, halving the number of checks. Larger m, such as 24, can reduce checks further by limiting valid classes to about one-third or less of all possibilities, depending on n \pmod{m}. The sieving condition can be expressed as the requirement that there exists an b satisfying b^2 \equiv a^2 - n \pmod{m} for the residue class of a. Precomputing the valid classes incurs a one-time setup cost proportional to m, which is negligible for small fixed m (e.g., O(1) in practice), and amortizes over the potentially large search range up to O(\sqrt{n}) iterations in the worst case. This optimization can reduce the effective number of evaluations by 50% to 80% for typical semiprimes, with greater savings for larger m at the expense of more complex residue class management. Early suggestions for applying such sieving to Fermat's appear in the context of probabilistic factorization techniques around 1974.

Multiplier Methods

Multiplier methods enhance Fermat's factorization by introducing small integer multipliers k to scale the target number n, aiming to represent kn as a difference of squares where the squares are closer together, thus reducing the search space for unbalanced factorizations. This approach is particularly effective when the factors of n differ significantly in size, as the basic Fermat method performs poorly in such cases due to its O(n^{1/2}) complexity. The seminal contribution in this area is Lehman's algorithm, developed by R. Sherman Lehman in 1974, which systematically selects multipliers up to approximately n^{1/3} to achieve a deterministic factorization in O(n^{1/3}) time. Lehman's algorithm begins with trial division to check for small factors up to n^{1/3}, eliminating trivial cases efficiently. For each multiplier k from 1 to \lceil n^{1/3} \rceil, it computes m = \lceil \sqrt{kn} \rceil and searches for integers a starting from m up to roughly m + n^{1/6}/(4\sqrt{k}). For each such a, it checks whether a^2 - kn is a perfect square b^2; if so, the factors of n can be recovered via \gcd(a - b, n) and \gcd(a + b, n), as kn = (a - b)(a + b). This bounded search range ensures the overall time complexity remains O(n^{1/3}), a significant improvement over the basic method's exhaustive search up to O(n^{1/2}). The algorithm's strength lies in its ability to handle semiprimes with unbalanced factors, where one prime is much smaller than the other. For example, consider n = 2581 = 29 \times 89; the basic Fermat method requires searching far from \sqrt{n} \approx 50.8, but with k=3, kn = 7743, and testing a near \sqrt{7743} \approx [88](/page/88) yields a=[88](/page/88), b=1 since $88^2 - 1^2 = 7743, allowing recovery of the factors via gcd computations. Lehman's method has been implemented and tested on numbers up to 21 digits using 1970s hardware, demonstrating practical viability for its era. While effective standalone, multiplier methods like Lehman's can be combined with sieving techniques to further optimize by pre-filtering potential b^2 values, though this integration is typically addressed in sieving-focused optimizations.

Advanced Variants and Modern Applications

Dixon's random squares method, introduced in 1981, represents an early advanced variant that extends Fermat's approach by incorporating a base of small primes and selecting random integers whose squares modulo n are over this base. This probabilistic technique generates relations to solve for pairs satisfying x^2 \equiv y^2 \pmod{n} with x \not\equiv \pm y \pmod{n}, yielding a non-trivial via \gcd(x-y, n). By using multipliers and sieving to identify values, it handles larger composites more efficiently than basic Fermat, achieving subexponential and serving as a direct precursor to the . Fermat's method integrates into modern general-purpose factorization tools like the (QS) and (GNFS) as a specialized case for semiprimes with closely spaced factors, where it acts as an initial screening step before invoking the full sieve. In implementations such as msieve, an open-source QS and GNFS tool, Fermat is applied early to detect if |p - q| is small relative to \sqrt{n}, avoiding unnecessary sieving overhead; this is particularly effective for RSA moduli vulnerable to close-prime generation flaws. Parallel variants distribute the search for suitable a values in n = a^2 - b^2, achieving speedups of up to 6.7 times on multi-core systems with 12 processors by partitioning the space across threads. Self-initializing extensions, akin to adaptations in quadratic sieving for , have been explored for GPU environments, though Fermat-specific GPU implementations remain niche due to the method's sequential nature for large searches. In contemporary , Fermat's method targets weak keys generated with close primes, as demonstrated in scans of keys where flawed generation led to factorable moduli; for instance, vulnerabilities like those in printer (CVE-2022-26320) were exploited using Fermat to recover private keys from ones in seconds on standard hardware. It serves as an educational tool in courses to illustrate difference-of-squares representations and the importance of prime separation in , often implemented in simple scripts for classroom demonstrations of factoring semiprimes up to 100 bits. For niche applications in systems, Fermat finds use in resource-constrained devices for factoring small-to-medium integers (under 512 bits) without requiring extensive , outperforming while remaining . Regarding quantum resistance, Fermat remains a classical unaffected by Shor's quantum factoring routine, though its inherent slowness for large general cases limits its role in post-quantum scenarios to verifying special-case vulnerabilities. Compared to the elliptic curve method (), Fermat excels when factors are extremely close (e.g., |p - q| < 2^{0.1 \log_2 n}), as ECM is statistically tuned for medium-sized factors around 40-60 bits but less deterministic for ultra-close pairs near \sqrt{n}. As an example, for a 1024-bit RSA modulus where |p - q| < 2^{20}, Fermat factorization requires a negligible number of iterations (approximately \frac{(q - p)^2}{8 \sqrt{n}} \ll 1), completing in microseconds on a single core, making it viable as a preprocessing step before QS or GNFS; this contrasts with general 1024-bit factoring, which requires massive computation via sieving methods.

References

  1. [1]
    Fermat's Factorization Method -- from Wolfram MathWorld
    Fermat's factorization methods look for integers x and y such that n=x^2-y^2. Then n=(xy)(x+y) and n is factored.
  2. [2]
    [PDF] The Fermat Factorization Method - Trinity University
    Remark: The Fermat method can be applied to arbitrary odd n to try to find a divisor/complementary divisor pair that are relatively.Missing: mathematical | Show results with:mathematical
  3. [3]
    None
    ### Fermat's Factorization Method: History, Description, and Key Developments
  4. [4]
    [PDF] History of integer factorization - Purdue Computer Science
    1.1.2 Fermat's Difference of Squares Method. Fermat's Difference of Squares Method may be used to factor an odd number n by expressing n as a difference of ...
  5. [5]
    A Brief History of Factoring and Primality Testing B. C. (Before ... - jstor
    n is composite (primality test) before applying a factorization algorithm. In 1643, Fermat developed a method for factoring that was based on a simple ob-.
  6. [6]
    Pierre Fermat (1601 - 1665) - Biography - MacTutor
    He wrote angrily to Fermat but although Fermat gave more details in his reply, Frenicle de Bessy felt that Fermat was almost teasing him.Missing: factorization | Show results with:factorization
  7. [7]
    [PDF] The Fermat factorization method revisited - Cryptology ePrint Archive
    Jun 30, 2009 · We consider the well known Fermat factorization method, we call the Fermat factorization equation the equation solved by it: P(x, y) = (x + 2R)2 ...
  8. [8]
    [PDF] Cracking RSA with Various Factoring Algorithms
    Oct 2, 2020 · In the 1640s, Pierre de Fermat devised what is known as Fermat's factorization method. Over one century later,. Leonhard Euler improved upon ...
  9. [9]
    [PDF] Revisiting Fermat's Factorization Method
    Oct 21, 2024 · Fermat's method works efficiently if P and Q are close to each other, i.e., when P ≈ Q. The algorithm proceeds by starting with x = d. √. Ne.
  10. [10]
    [PDF] 1 Introduction 2 Extended Fermat Method - arXiv
    The earliest factoring algorithm based on this identity appears to be the Fermat method, also known as the difference of squares method, [RL, p. 147].Missing: derivation | Show results with:derivation<|control11|><|separator|>
  11. [11]
    Integer factorization - Algorithms for Competitive Programming
    May 29, 2025 · Fermat's factorization method¶ ; = p ⋅ q · $n = p \cdot q$ as the difference of two squares ; = a 2 − b 2 · $n = a^2 - b^2$ :.
  12. [12]
    [PDF] Integer Factorization Algorithms - Connelly Barnes
    Dec 7, 2004 · Pseudocode: Fermat Factorization. 6. 5. Algorithm: Pollard rho ... Pseudocode: Brent's Factorization Method. 8. 9. Algorithm: Pollard p-1 ...
  13. [13]
    [PDF] Math 5330 Spring 2018 Elementary factoring algorithms The RSA ...
    steps, Fermat's method takes 4,999,900,000 steps. On average, one expects to find a composite number n to have a prime divisor of size n.63, and coprime part of ...<|control11|><|separator|>
  14. [14]
    SPEEDING FERMAT'S FACTORING METHOD 1. Introduction Let n ...
    Mar 1, 1999 · It is used for subsidiary factorisations in certain other factoring methods, such as the quadratic sieve. In this paper, another factoring ...
  15. [15]
    RSA cryptanalysis — Fermat factorization exact bound and the role ...
    We analyse the Fermat factorization theorem and show why it can factorize numbers in constant time complexity (i.e. O ( 1 ) ) if the difference is slight. We ...
  16. [16]
    Factoring Large Integers - American Mathematical Society
    ... 1974, PAGES 637-646. Factoring Large Integers. By R. Sherman Lehman. Abstract. A modification of Fermat's difference of squares method is used for factoring.
  17. [17]
    Lehman's Factoring Algorithm - Programming Praxis
    Aug 22, 2017 · Let's begin by recapping Fermat's algorithm. It starts by taking the smallest integer larger than the square root of the number n being factored ...
  18. [18]
    [PDF] Asymptotically Fast Factorization of Integers - cs.wisc.edu
    Asymptotically Fast Factorization of Integers. By John D. Dixon*. Abstract. The paper describes a "probabilistic algorithm" for finding a factor of any large.
  19. [19]
    Fermat Factorization in the Wild - Cryptology ePrint Archive
    Jan 9, 2023 · A flawed RSA key generation function that produces close primes can therefore be attacked with Fermat's factorization. We discovered a small ...Missing: ROCA | Show results with:ROCA
  20. [20]
    [PDF] Fermat Factorization using a Multi-Core System
    In this study, we focus on the Fermat factorization (FF) algorithm, which is an efficient method when the difference between two factors is small. Many ...
  21. [21]
    Fermat Attack - badkeys
    If RSA keys are generated with "close" primes then RSA can be broken with Fermat's factorization algorithm. During the development of badkeys.info such ...Missing: ROCA | Show results with:ROCA
  22. [22]
    RSA Weakness: Fermat's Attack - ASecuritySite.com
    Pierre de Fermat defined a factorization method which could factor prime number factors, if these prime numbers were close to each other. In RSA, we use a ...Missing: ROCA | Show results with:ROCA