Search for a tool
Fermat Factorization

Tool for factoring odd integers, applying Fermat's method, understanding the difference of squares, and quickly calculating non-trivial factors.

Results

Fermat Factorization -

Tag(s) : Arithmetics

Share
Share
dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!


Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!


Feedback and suggestions are welcome so that dCode offers the best 'Fermat Factorization' tool for free! Thank you!

Fermat Factorization

Fermat's Factorization Calculator


Answers to Questions (FAQ)

What is Fermat factorization? (Definition)

Fermat factorization is a method for factoring an odd, composite (non-prime) natural number $ N $. It relies on the algebraic identity of the difference of two squares: $$ N = a^2 - b^2 = (a + b)(a - b) $$

The goal is to find two integers $ a $ and $ b $ such that $ a^2 - N $ is a perfect square $ b $.

When this is the case, this expression immediately yields two non-trivial factors of $ N $, namely $ c = (a+b) $ and $ d = (a-b) $

Why use Fermat factorization?

Fermat factorization is particularly efficient when $ N $ has two factors $ c $ and $ d $ that are close to each other, that is, when $ c \pm \epsilon_1 \approx d \pm \epsilon_2 \approx \sqrt{N} $.

In this case, the difference $ a^2 - N $ quickly becomes a perfect square.

Conversely, when $ N $ is prime or when its factors are very far from $ \sqrt{N} $, the method becomes inefficient and significantly less effective than other factorization algorithms.

How does Fermat's factorization algorithm work?

Fermat's factorization algorithm applies the following steps:

1 - Initialize $ A $ to the smallest integer value such that $ A \ge \sqrt{N} $

2 - Calculate $ B^2 = A^2 - N $

3 - Test if $ B^2 $ is a perfect square

4 - If so, then $ N = (A+B)(A-B) $ and stop. Otherwise, increment $ A $ by 1 and repeat the calculation in step 2

Example: $ N = 5893 $, then $ A = 77 $ and $ B^2 = 77^2 - 5893 = 36 $, therefore $ B = 6 $. The factorization is then: $ 5893 = (77 + 6)(77 - 6) = 83 \times 71 $

What is the complexity of the algorithm?

The complexity of Fermat's factorization depends on the difference between the factors of $ N $.

— Favorable case: if $ N = c \times d $ with $ c \approx d \approx \sqrt{N} $, the number of iterations is small, often proportional to $ |d - c| $.

— Worst-case: if $ N $ is prime, the algorithm must test all integers up to $ \sqrt{N} $, making the method impractical for large numbers.

How to implement Fermat's method?

Here is the source code and a Python implementation:// Pseudo code

function fermat_factorization(N) {

if N is even {

return (2, N / 2)

}

A ← ceil(sqrt(N))

while true {

B2 ← A^2 - N

if B2 is a perfect square {

B ← sqrt(B2)

return (A - B, A + B)

}

A ← A + 1

}

}



// Python

import math

def fermat_factorization(N):

A = math.isqrt(N)

while True:

B2 = A * A - N

B = math.isqrt(B2)

if B * B == B2:

return (A - B, A + B)

A += 1

Source code

dCode retains ownership of the "Fermat Factorization" source code. Any algorithm for the "Fermat Factorization" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Fermat Factorization" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Fermat Factorization" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.

Cite dCode

The content of the page "Fermat Factorization" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).

Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).

To cite dCode.fr on another website, use the link: https://www.dcode.fr/fermat-factorization

In a scientific article or book, the recommended bibliographic citation is: Fermat Factorization on dCode.fr [online website], retrieved on 2026-01-19, https://www.dcode.fr/fermat-factorization

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Fermat Factorization' tool for free! Thank you!


https://www.dcode.fr/fermat-factorization
© 2026 dCode — The ultimate collection of tools for games, math, and puzzles.
 
Feedback