Skip to content

Commit

Permalink
move PollardRhoBrent31Runner to test scope
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Dec 29, 2024
1 parent 85186a9 commit 664cb90
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
package de.tilman_neumann.jml.factor.pollardRho;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.SecureRandom;

Expand All @@ -24,8 +21,6 @@

import de.tilman_neumann.jml.factor.FactorAlgorithm;
import de.tilman_neumann.jml.gcd.Gcd31;
import de.tilman_neumann.util.ConfigUtil;
import de.tilman_neumann.util.SortedMultiset;

/**
* Brents's improvement of Pollard's Rho algorithm, following [Richard P. Brent: An improved Monte Carlo Factorization Algorithm, 1980].
Expand All @@ -36,6 +31,7 @@
*/
public class PollardRhoBrent31 extends FactorAlgorithm {
private static final Logger LOG = LogManager.getLogger(PollardRhoBrent31.class);
private static final boolean DEBUG = false;
private static final SecureRandom RNG = new SecureRandom();

private int N;
Expand Down Expand Up @@ -83,21 +79,21 @@ public int findSingleFactor(int N) {
G = gcd.gcd(q, N);
// if q==0 then G==N -> the loop will be left and restarted with new x0, c
k += m;
//LOG.info("r = " + r + ", k = " + k);
if (DEBUG) LOG.debug("r = " + r + ", k = " + k);
} while (k<r && G==1);
r <<= 1;
//LOG.info("r = " + r + ", G = " + G);
if (DEBUG) LOG.debug("r = " + r + ", G = " + G);
} while (G==1);
if (G==N) {
do {
ys = addModN(squareModN(ys), c);
int diff = x<ys ? ys-x : x-ys;
G = gcd.gcd(diff, N);
} while (G==1);
//LOG.info("G = " + G);
if (DEBUG) LOG.debug("G = " + G);
}
} while (G==N);
//LOG.debug("Found factor of " + N + " = " + factor);
if (DEBUG) LOG.debug("Found factor of " + N + " = " + G);
return G;
}

Expand All @@ -120,32 +116,4 @@ private int addModN(int a, int b) {
private int squareModN(long x) {
return (int) ((x * x) % N);
}

/**
* Test.
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();

while(true) {
String input;
try {
LOG.info("Please insert the integer to factor:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
input = line.trim();
LOG.debug("factoring " + input + "...");
} catch (IOException ioe) {
LOG.error("io-error occurring on input: " + ioe.getMessage());
continue;
}

long start = System.currentTimeMillis();
BigInteger n = new BigInteger(input);
SortedMultiset<BigInteger> result = new PollardRhoBrent31().factor(n);
LOG.info("Factored " + n + " = " + result.toString() + " in " + (System.currentTimeMillis()-start) + " ms");

} // next input...
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
package de.tilman_neumann.jml.factor.pollardRho;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.tilman_neumann.util.ConfigUtil;
import de.tilman_neumann.util.SortedMultiset;

public class PollardRhoBrent31Runner {
private static final Logger LOG = LogManager.getLogger(PollardRhoBrent31Runner.class);

/**
* Test.
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();

while(true) {
String input;
try {
LOG.info("Please insert the integer to factor:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
input = line.trim();
LOG.debug("factoring " + input + "...");
} catch (IOException ioe) {
LOG.error("io-error occurring on input: " + ioe.getMessage());
continue;
}

long start = System.currentTimeMillis();
BigInteger n = new BigInteger(input);
SortedMultiset<BigInteger> result = new PollardRhoBrent31().factor(n);
LOG.info("Factored " + n + " = " + result.toString() + " in " + (System.currentTimeMillis()-start) + " ms");

} // next input...
}
}

0 comments on commit 664cb90

Please sign in to comment.