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 PrinterStateReasons API
Summing Numbers with Java Streams
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement LinkedBlockingDeque API
Object Type Casting in Java
Encode a String to UTF-8 in Java
A Guide to Java HashMap
Mapping Nested Values with Jackson
Lớp LinkedHashMap trong Java
Guide to @JsonFormat in Jackson
Spring Boot - Code Structure
Java Program to Implement Binomial Tree
Introduction to Spring Boot CLI
Java Program to Find All Pairs Shortest Path
The DAO with JPA and Spring
Java Program to Implement Gauss Seidel Method
Phương thức forEach() trong java 8
Filtering and Transforming Collections in Guava
Spring Security and OpenID Connect
Java Program to find the peak element of an array using Binary Search approach
Generating Random Numbers in a Range in Java
Spring Webflux and CORS
Java Program to Describe the Representation of Graph using Adjacency Matrix
Java Program to Implement RoleList API
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Một số nguyên tắc, định luật trong lập trình
Unsatisfied Dependency in Spring
Java Program to Generate Random Hexadecimal Byte
An Intro to Spring Cloud Contract
Jackson vs Gson
Java Program to Implement Depth-limited Search
Tạo ứng dụng Java RESTful Client với thư viện Retrofit