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 Construct an Expression Tree for an Infix Expression
Java Program to Implement Hopcroft Algorithm
An Introduction to ThreadLocal in Java
The Registration Process With Spring Security
Java Program to Implement Interval Tree
What is a POJO Class?
Java Program to Implement HashMap API
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
RegEx for matching Date Pattern in Java
Spring Boot - Exception Handling
Java Program to Implement Weight Balanced Tree
So sánh HashMap và Hashtable trong Java
Java Program to Implement EnumMap API
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring Cloud Connectors and Heroku
Spring Boot - Quick Start
Convert XML to JSON Using Jackson
Java Program to Implement Segment Tree
Find the Registered Spring Security Filters
Java Program to Describe the Representation of Graph using Adjacency Matrix
Java IO vs NIO
Control the Session with Spring Security
Java Program to Implement Counting Sort
Java Program to Implement Shell Sort
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Implement Park-Miller Random Number Generation Algorithm
Upload and Display Excel Files with Spring MVC
Cachable Static Assets with Spring MVC