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:
Spring Cloud – Tracing Services with Zipkin
Handling Errors in Spring WebFlux
Java Program to Implement Find all Back Edges in a Graph
Java Program to Create a Random Graph Using Random Edge Generation
Consuming RESTful Web Services
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Byte Array to InputStream
So sánh HashMap và Hashtable trong Java
Java Program to Implement a Binary Search Tree using Linked Lists
Guide to the Synchronized Keyword in Java
Spring NoSuchBeanDefinitionException
Adding a Newline Character to a String in Java
Implementing a Binary Tree in Java
Guide to BufferedReader
Java Program to Implement Find all Cross Edges in a Graph
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Implement Sorted List
Java Program to Check whether Directed Graph is Connected using DFS
XML Serialization and Deserialization with Jackson
Spring @RequestParam Annotation
Java Program to Implement Jarvis Algorithm
Java Program to Implement Dijkstra’s Algorithm using Set
Spring Security OAuth Login with WebFlux
Introduction to Spring Data REST
Java Program to Implement Range Tree
Instance Profile Credentials using Spring Cloud
Spring Cloud Connectors and Heroku
Weak References in Java
Logging in Spring Boot
Serve Static Resources with Spring
Entity To DTO Conversion for a Spring REST API