This is a Java Program to Implement Fermat Factorization Algorithm. Fermat’s factorization method, named after Pierre de Fermat, is based on the representation of an odd integer as the difference of two squares: N = a2 – b2. That difference is algebraically factorable as (a + b)(a – b); if neither factor equals one, it is a proper factorization of N.
Here is the source code of the Java Program to Implement Fermat Factorization Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to implement Fermat Factorization Algorithm
**/
import java.util.Scanner;
public class FermatFactorization
{
/** Fermat factor **/
public void FermatFactor(long N)
{
long a = (long) Math.ceil(Math.sqrt(N));
long b2 = a * a - N;
while (!isSquare(b2))
{
a++;
b2 = a * a - N;
}
long r1 = a - (long)Math.sqrt(b2);
long r2 = N / r1;
display(r1, r2);
}
/** function to display roots **/
public void display(long r1, long r2)
{
System.out.println("\nRoots = "+ r1 +" , "+ r2);
}
/** function to check if N is a perfect square or not **/
public boolean isSquare(long N)
{
long sqr = (long) Math.sqrt(N);
if (sqr * sqr == N || (sqr + 1) * (sqr + 1) == N)
return true;
return false;
}
/** main method **/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Fermat Factorization Test\n");
System.out.println("Enter odd number");
long N = scan.nextLong();
FermatFactorization ff = new FermatFactorization();
ff.FermatFactor(N);
}
}
Output:
Fermat Factorization Test Enter odd number 5959 Roots = 59 , 101 Fermat Factorization Test Enter odd number 432633 Roots = 499 , 867
Related posts:
Marker Interface trong Java
Java – Convert File to InputStream
Introduction to Spring Cloud CLI
Java Program to Implement K Way Merge Algorithm
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Generate Random Numbers Using Multiply with Carry Method
Jackson Exceptions – Problems and Solutions
Java Program to Implement Weight Balanced Tree
Spring Boot - Code Structure
Notify User of Login From New Device or Location
Spring Boot - Actuator
Weak References in Java
Java Program to Implement Uniform-Cost Search
Java Program to Implement Hopcroft Algorithm
DynamoDB in a Spring Boot Application Using Spring Data
Create a Custom Auto-Configuration with Spring Boot
Spring Security Form Login
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Database Migrations with Flyway
Hướng dẫn sử dụng Printing Service trong Java
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Max-Flow Min-Cut Theorem
New Stream Collectors in Java 9
Java Program to Implement LinkedHashSet API
Java Program to Implement Knapsack Algorithm
Updating your Password
Java Program to Compute Determinant of a Matrix
Chuyển đổi giữa các kiểu dữ liệu trong Java
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Bloom Filter
Spring Boot - Exception Handling
Java Program to Print only Odd Numbered Levels of a Tree