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 Vigenere Cypher
Jackson – Change Name of Field
Control Structures in Java
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Perform Quick Sort on Large Number of Elements
The Order of Tests in JUnit
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java InputStream to String
Returning Image/Media Data with Spring MVC
Spring MVC and the @ModelAttribute Annotation
@Order in Spring
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring REST API + OAuth2 + Angular
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Bubble Sort
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
A Custom Data Binder in Spring MVC
Guide to UUID in Java
Spring Boot - Google OAuth2 Sign-In
Login For a Spring Web App – Error Handling and Localization
Concurrent Test Execution in Spring 5
Guide to the Java Clock Class
Java CyclicBarrier vs CountDownLatch
Hướng dẫn Java Design Pattern – Interpreter
Spring REST API with Protocol Buffers
So sánh Array và ArrayList trong Java
Java Program to Check Whether Graph is DAG
Spring REST API + OAuth2 + Angular
How to Get the Last Element of a Stream in Java?
Java Program to Perform Arithmetic Operations on Numbers of Size
An Introduction to ThreadLocal in Java
Hướng dẫn sử dụng luồng vào ra ký tự trong Java