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:
A Guide to Java SynchronousQueue
Từ khóa this và super trong Java
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Adjacency List
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Find Strongly Connected Components in Graphs
Java Program to Find Number of Articulation points in a Graph
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Testing in Spring Boot
Guide to Spring 5 WebFlux
Hướng dẫn Java Design Pattern – Intercepting Filter
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Jackson – Decide What Fields Get Serialized/Deserialized
Thao tác với tập tin và thư mục trong Java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Filtering and Transforming Collections in Guava
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement Queue using Two Stacks
Testing an OAuth Secured API with Spring MVC
Introduction to the Java ArrayDeque
Java Program to Implement Queue using Linked List
The XOR Operator in Java
Apache Commons Collections Bag
The HttpMediaTypeNotAcceptableException in Spring MVC
Hướng dẫn Java Design Pattern – Strategy
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement Fibonacci Heap
Spring WebClient Requests with Parameters
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Handle EML file with JavaMail
Introduction to Java 8 Streams
Java Program to Implement Network Flow Problem