This is a Java Program to Implement Pollard Rho Algorithm. Pollard Rho algorithm is a general purpose factorization algorithm. It is particularly effective at splitting composite numbers with small factors.
Here is the source code of the Java Program to Implement Pollard Rho Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/** ** Java Program to implement Pollard Rho Algorithm **/ import java.util.Scanner; /** Class PollardRho **/ public class PollardRho { private static final long C = 1; /** function X * X + C, change value of C as required **/ private long f(long X) { return X * X + C; } /** get divisor **/ private long rho(long N) { long x1 = 2, x2 = 2, divisor; if (N % 2 == 0) return 2; do { x1 = f(x1) % N; x2 = f(f(x2)) % N; divisor = gcd(Math.abs(x1 - x2), N); } while (divisor == 1); /** return divisor **/ return divisor; } /** GCD of two numbers **/ public long gcd(long p, long q) { if (p % q == 0) return q; return gcd(q, p % q); } /** Check if num is prime **/ public boolean isPrime(long N) { for (int i = 2; i <= Math.sqrt(N); i++) if (N % i == 0) return false; return true; } /** get all factors **/ public void factor(long N) { if (N == 1) return; if (isPrime(N)) { System.out.println(N); return; } long divisor = rho(N); factor(divisor); factor(N / divisor); } /** Main function **/ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Pollard Rho Algorithm\n"); System.out.println("Enter a number"); long N = scan.nextLong(); System.out.println("\nFactors are : "); PollardRho pr = new PollardRho(); pr.factor (N); } }
Output:
Pollard Rho Algorithm Enter a number 2406 Factors are : 2 3 401
Related posts:
Java Program to Solve a Matching Problem for a Given Specific Case
Extract links from an HTML page
Composition, Aggregation, and Association in Java
Java Program to Create the Prufer Code for a Tree
Introduction to Spring Cloud Stream
Map Interface trong java
A Comparison Between Spring and Spring Boot
New Features in Java 12
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Spring MVC and the @ModelAttribute Annotation
Java Program to Implement Adjacency List
Using a Spring Cloud App Starter
Java Program to Compute Determinant of a Matrix
Java Program to Implement Hash Trie
Logout in an OAuth Secured Application
XML-Based Injection in Spring
Java Program to Implement Segment Tree
Java Program to Check for balanced parenthesis by using Stacks
Spring Boot Integration Testing with Embedded MongoDB
Checking for Empty or Blank Strings in Java
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Patricia Trie
String Joiner trong Java 8
Guide to the Java TransferQueue
Java Program to Implement Booth Algorithm
Java Program to Generate Random Hexadecimal Byte
Java Program to Perform String Matching Using String Library
Một số ký tự đặc biệt trong Java
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Getting a File’s Mime Type in Java
Spring Security 5 for Reactive Applications