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 the Hill Cypher
HttpClient 4 – Send Custom Cookie
Java Program to Implement Gabow Algorithm
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
A Guide to BitSet in Java
Tạo số và chuỗi ngẫu nhiên trong Java
Composition, Aggregation, and Association in Java
Jackson Ignore Properties on Marshalling
Java Program to Implement Graph Coloring Algorithm
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Introduction to Apache Commons Text
Spring Data MongoDB – Indexes, Annotations and Converters
Java Program to Perform Searching Using Self-Organizing Lists
Spring Security with Maven
Circular Dependencies in Spring
Java Program to Implement Bucket Sort
Lập trình mạng với java
Java Program to implement Bit Set
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
How to Return 404 with Spring WebFlux
Instance Profile Credentials using Spring Cloud
Hướng dẫn sử dụng String Format trong Java
Java Program to Implement Shunting Yard Algorithm
So sánh HashMap và HashSet trong Java
What is Thread-Safety and How to Achieve it?
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Autoboxing và Unboxing trong Java
Returning Image/Media Data with Spring MVC