mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
@@ -3,12 +3,12 @@ package com.thealgorithms.maths;
|
||||
/*
|
||||
* Java program for pollard rho algorithm
|
||||
* The algorithm is used to factorize a number n = pq,
|
||||
* where p is a non-trivial factor.
|
||||
* where p is a non-trivial factor.
|
||||
* Pollard's rho algorithm is an algorithm for integer factorization
|
||||
* and it takes as its inputs n, the integer to be factored;
|
||||
* and it takes as its inputs n, the integer to be factored;
|
||||
* and g(x), a polynomial in x computed modulo n.
|
||||
* In the original algorithm, g(x) = ((x ^ 2) − 1) mod n,
|
||||
* but nowadays it is more common to use g(x) = ((x ^ 2) + 1 ) mod n.
|
||||
* but nowadays it is more common to use g(x) = ((x ^ 2) + 1 ) mod n.
|
||||
* The output is either a non-trivial factor of n, or failure.
|
||||
* It performs the following steps:
|
||||
* x ← 2
|
||||
@@ -20,19 +20,20 @@ package com.thealgorithms.maths;
|
||||
* y ← g(g(y))
|
||||
* d ← gcd(|x - y|, n)
|
||||
|
||||
* if d = n:
|
||||
* if d = n:
|
||||
* return failure
|
||||
* else:
|
||||
* return d
|
||||
|
||||
* Here x and y corresponds to xi and xj in the previous section.
|
||||
* Here x and y corresponds to xi and xj in the previous section.
|
||||
* Note that this algorithm may fail to find a nontrivial factor even when n is composite.
|
||||
* In that case, the method can be tried again, using a starting value other than 2 or a different g(x)
|
||||
*
|
||||
* In that case, the method can be tried again, using a starting value other than 2 or a different
|
||||
g(x)
|
||||
*
|
||||
* Wikipedia: https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
|
||||
*
|
||||
*
|
||||
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
*
|
||||
*
|
||||
* */
|
||||
public class PollardRho {
|
||||
|
||||
@@ -40,7 +41,8 @@ public class PollardRho {
|
||||
* This method returns a polynomial in x computed modulo n
|
||||
*
|
||||
* @param base Integer base of the polynomial
|
||||
* @param modulus Integer is value which is to be used to perform modulo operation over the polynomial
|
||||
* @param modulus Integer is value which is to be used to perform modulo operation over the
|
||||
* polynomial
|
||||
* @return Integer (((base * base) - 1) % modulus)
|
||||
*/
|
||||
static int g(int base, int modulus) {
|
||||
@@ -57,13 +59,13 @@ public class PollardRho {
|
||||
static int pollardRho(int number) {
|
||||
int x = 2, y = 2, d = 1;
|
||||
while (d == 1) {
|
||||
//tortoise move
|
||||
// tortoise move
|
||||
x = g(x, number);
|
||||
|
||||
//hare move
|
||||
// hare move
|
||||
y = g(g(y, number), number);
|
||||
|
||||
//check GCD of |x-y| and number
|
||||
// check GCD of |x-y| and number
|
||||
d = GCD.gcd(Math.abs(x - y), number);
|
||||
}
|
||||
if (d == number) {
|
||||
|
||||
Reference in New Issue
Block a user