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:
Java Program to Implement Flood Fill Algorithm
Hướng dẫn Java Design Pattern – Bridge
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Check Cycle in a Graph using Topological Sort
Registration with Spring Security – Password Encoding
Chương trình Java đầu tiên
Java Program for Topological Sorting in Graphs
New Features in Java 13
Guide to java.util.concurrent.BlockingQueue
Java Program to Implement Sparse Array
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring 5 Testing with @EnabledIf Annotation
Deploy a Spring Boot App to Azure
HttpAsyncClient Tutorial
Spring Boot - Securing Web Applications
Spring Boot - CORS Support
The Java 8 Stream API Tutorial
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Implement ScapeGoat Tree
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Check If a File or Directory Exists in Java
Control the Session with Spring Security
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement VList
Java Program to Implement Direct Addressing Tables
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java String Conversions
Adding Shutdown Hooks for JVM Applications
The Registration Process With Spring Security
Giới thiệu Design Patterns
Reactive WebSockets with Spring 5
Registration – Activate a New Account by Email