Skip to content

Commit

Permalink
move FactorSieve main() method to test scope
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Dec 30, 2024
1 parent a84adb3 commit 4f3745f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
35 changes: 1 addition & 34 deletions src/main/java/de/tilman_neumann/jml/factor/FactorSieve.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package de.tilman_neumann.jml.factor;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

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

import de.tilman_neumann.jml.primes.exact.SegmentedSieve;
import de.tilman_neumann.jml.primes.exact.SieveCallback;
import de.tilman_neumann.util.Ensure;
import de.tilman_neumann.util.ConfigUtil;
import de.tilman_neumann.util.SortedMultiset;
import de.tilman_neumann.util.SortedMultiset_BottomUp;

Expand Down Expand Up @@ -104,7 +101,7 @@ public Map<Long, SortedMultiset<Long>> getFactorizations() {
return factorizations;
}

private static long computeProduct(SortedMultiset<Long> factors) {
public static long computeProduct(SortedMultiset<Long> factors) {
if (factors == null) return 0;

long result = 1;
Expand All @@ -114,34 +111,4 @@ private static long computeProduct(SortedMultiset<Long> factors) {
}
return result;
}

public static void main(String[] args) {
ConfigUtil.initProject();

long start = 99000000, limit = 100000000;
FactorSieve sieve = new FactorSieve(start, limit);
long t0 = System.currentTimeMillis();
sieve.sieve();
long t1 = System.currentTimeMillis();
LOG.info("Factoring all numbers from " + start + " to " + limit + " using the sieve took " + (t1-t0) + " milliseconds.");
if (DEBUG) {
for (long n=start; n<=limit; n++) { // n==1 gives null factors
SortedMultiset<Long> factors = sieve.getFactorization(n);
LOG.info(n + " = " + factors);
if (n>1) {
long test = computeProduct(factors);
Ensure.ensureEquals(n, test);
}
}
}

// without batch
FactorAlgorithm factorizer = FactorAlgorithm.getDefault();
long t2 = System.currentTimeMillis();
for (long n=start; n<=limit; n++) {
factorizer.factor(BigInteger.valueOf(n));
}
long t3 = System.currentTimeMillis();
LOG.info("Factoring all numbers from " + start + " to " + limit + " individually took " + (t3-t2) + " milliseconds.");
}
}
58 changes: 58 additions & 0 deletions src/test/java/de/tilman_neumann/jml/factor/FactorSieveDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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;

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.Ensure;
import de.tilman_neumann.util.SortedMultiset;

public class FactorSieveDemo {
private static final Logger LOG = LogManager.getLogger(FactorSieveDemo.class);
private static final boolean DEBUG = false;

public static void main(String[] args) {
ConfigUtil.initProject();

long start = 99000000, limit = 100000000;
FactorSieve sieve = new FactorSieve(start, limit);
long t0 = System.currentTimeMillis();
sieve.sieve();
long t1 = System.currentTimeMillis();
LOG.info("Factoring all numbers from " + start + " to " + limit + " using the sieve took " + (t1-t0) + " milliseconds.");
if (DEBUG) {
for (long n=start; n<=limit; n++) { // n==1 gives null factors
SortedMultiset<Long> factors = sieve.getFactorization(n);
LOG.info(n + " = " + factors);
if (n>1) {
long test = FactorSieve.computeProduct(factors);
Ensure.ensureEquals(n, test);
}
}
}

// without batch
FactorAlgorithm factorizer = FactorAlgorithm.getDefault();
long t2 = System.currentTimeMillis();
for (long n=start; n<=limit; n++) {
factorizer.factor(BigInteger.valueOf(n));
}
long t3 = System.currentTimeMillis();
LOG.info("Factoring all numbers from " + start + " to " + limit + " individually took " + (t3-t2) + " milliseconds.");
}
}

0 comments on commit 4f3745f

Please sign in to comment.