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 Self Balancing Binary Search Tree
A Guide to Spring Cloud Netflix – Hystrix
DistinctBy in the Java Stream API
Spring Boot - Web Socket
Mockito and JUnit 5 – Using ExtendWith
Guide to @JsonFormat in Jackson
Error Handling for REST with Spring
Java Optional as Return Type
Practical Java Examples of the Big O Notation
Hướng dẫn Java Design Pattern – Mediator
Spring Boot - Twilio
Java Program to Implement Binary Search Tree
Java Program to Implement Sieve Of Eratosthenes
Sending Emails with Java
How to Count Duplicate Elements in Arraylist
Lấy ngày giờ hiện tại trong Java
Java Program to Implement Solovay Strassen Primality Test Algorithm
Hướng dẫn sử dụng lớp Console trong java
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Introduction to Apache Commons Text
Java Program to Create a Random Linear Extension for a DAG
Quick Guide to the Java StringTokenizer
Quick Guide to @RestClientTest in Spring Boot
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Bellman-Ford Algorithm
Java Program to Find the Edge Connectivity of a Graph
A Guide to Java HashMap
Guide to Spring @Autowired
Disable DNS caching
Immutable Map Implementations in Java
Get and Post Lists of Objects with RestTemplate
JUnit 5 for Kotlin Developers