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:
Connect through a Proxy
Java Program to Generate Random Numbers Using Probability Distribution Function
Cài đặt và sử dụng Swagger UI
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Create the Prufer Code for a Tree
A Guide to Java HashMap
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Randomized Binary Search Tree
How to Read a Large File Efficiently with Java
Java Program to Implement Splay Tree
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Program to Implement Repeated Squaring Algorithm
Java Program to Check whether Undirected Graph is Connected using BFS
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
How to Get All Spring-Managed Beans?
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Apache Camel with Spring Boot
Spring Boot Integration Testing with Embedded MongoDB
HttpClient 4 – Follow Redirects for POST
Registration with Spring Security – Password Encoding
REST Web service: Basic Authentication trong Jersey 2.x
Spring Security 5 for Reactive Applications
HttpAsyncClient Tutorial
Spring Boot: Customize the Jackson ObjectMapper
Finding the Differences Between Two Lists in Java
Java Program to Find Basis and Dimension of a Matrix
Java 8 Stream findFirst() vs. findAny()
Tính kế thừa (Inheritance) trong java
Guide to java.util.Formatter
Generate Spring Boot REST Client with Swagger