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:
Introduction to Spring Method Security
Java Program to Implement Knapsack Algorithm
The Difference Between Collection.stream().forEach() and Collection.forEach()
An Introduction to ThreadLocal in Java
Java Program to Implement Heap
Hướng dẫn Java Design Pattern – MVC
Difference Between Wait and Sleep in Java
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Jackson Ignore Properties on Marshalling
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Find all Back Edges in a Graph
CharSequence vs. String in Java
Java Program to Find the Connected Components of an UnDirected Graph
Binary Numbers in Java
Lớp Collectors trong Java 8
DynamoDB in a Spring Boot Application Using Spring Data
Spring Boot - Bootstrapping
Converting Between Byte Arrays and Hexadecimal Strings in Java
A Guide to System.exit()
Guide to Guava Table
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Consuming RESTful Web Services
String Operations with Java Streams
Using a Spring Cloud App Starter
New Features in Java 8
Java Program to Perform Naive String Matching
Rate Limiting in Spring Cloud Netflix Zuul
A Guide to the ResourceBundle
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Sao chép các phần tử của một mảng sang mảng khác như thế nào?