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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Multiply Large Numbers using Grid Method
- Euler's Factorization method
- Fermat's Factorization Method
- Dixon's Factorization Method with implementation
- Sum of two large numbers
- LCM of two large numbers
- GCD of two numbers when one of them can be very large
- Difference of two large numbers
- Remainder with 7 for large numbers
- Divisible by 37 for large numbers
- Sum of first N natural numbers when N is extremely large
- Sum of all natural numbers from L to R ( for large values of L and R )
- Find Last Digit of a^b for Large Numbers
- Large Fibonacci Numbers in Java
- Sum of two large Floating-point numbers
- Sort an array of large numbers
- How to generate Large Prime numbers for RSA Algorithm
- Factorial of Large numbers using Logarithmic identity
- Writing power function for large numbers
- Multiply Large Numbers represented as Strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.