Fermat’s Factorization method for large numbers
Given a large number N, the task is to divide this number into a product of two factors, using Fermat’s Factorisation method.
Input: N = 105327569
Output: 10223, 10303
Input: N = 249803
Output: 23, 10861
Fermat Factorization: Fermat’s Factorization method is based on the representation of an odd integer as the difference of two squares.
For an integer N, we want a and b such as:
N = a2 - b2 = (a+b)(a-b) where (a+b) and (a-b) are the factors of the number N.
- Get the number as an object of BigInteger class
- Find the square root of N.
- It is guaranteed that the value of a is greater than sqrt(N) and value of b less than sqrt(N).
- Take the value of sqrt(n) as a and increment the number until and unless a number b is found such that N – a^2 is a perfect square.
Below is the implementation of the above approach:
- Time Complexity: O(sqrt(N))
- Space Complexity: O(1)