Skip to content

Commit

Permalink
move analyzer classes to test scope
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Dec 31, 2024
1 parent a7cd1de commit bb7ac05
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*
* @author Tilman Neumann
*/
public class PrimeCountsBetweenSquares implements SieveCallback {
private static final Logger LOG = LogManager.getLogger(PrimeCountsBetweenSquares.class);
public class PrimeCountsBetweenSquaresAnalyzer implements SieveCallback {
private static final Logger LOG = LogManager.getLogger(PrimeCountsBetweenSquaresAnalyzer.class);

private SegmentedSieve sieve;
private long limit;
Expand All @@ -38,7 +38,7 @@ public class PrimeCountsBetweenSquares implements SieveCallback {
private long s1;


public PrimeCountsBetweenSquares(long limit) {
public PrimeCountsBetweenSquaresAnalyzer(long limit) {
sieve = new SegmentedSieve(this);
this.limit = limit;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public void processPrime(long prime) {

public static void main(String[] args) {
ConfigUtil.initProject();
new PrimeCountsBetweenSquares(100000000000L).run();
new PrimeCountsBetweenSquaresAnalyzer(100000000000L).run();
// result: A014085 = 2, 2, 2, 3, 2, 4, 3, 4, 3, 5, 4, 5, 5, 4, 6, 7, 5, 6, 6, 7, 7, 7, ...
// In that entry we also see that a stronger conjecture exists:
// There is at least one prime in n^k...n^(k+1) with k = log(127)/log(16) ~ 1.747171172
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
*
* @author Tilman Neumann
*/
public class PrimeGapTest implements SieveCallback {
private static final Logger LOG = LogManager.getLogger(PrimeGapTest.class);
public class PrimeGapAnalyzer implements SieveCallback {
private static final Logger LOG = LogManager.getLogger(PrimeGapAnalyzer.class);

public static class StackElement {
public long lastPrime;
Expand All @@ -66,7 +66,7 @@ public StackElement(long lastPrime, long prime, double ratio) {
private long lastPrime = 2; // avoid "simulated" large relative prime gap
private int highestRankUpdate = Integer.MAX_VALUE;

public PrimeGapTest(long limit) {
public PrimeGapAnalyzer(long limit) {
stack = new Stack<StackElement>();
sieve = new SegmentedSieve(this);
this.limit = limit;
Expand Down Expand Up @@ -178,6 +178,6 @@ private void checkResult(StackElement elem, int i) {

public static void main(String[] args) {
ConfigUtil.initProject();
new PrimeGapTest(100000000000L).run();
new PrimeGapAnalyzer(100000000000L).run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import de.tilman_neumann.jml.precision.Scale;
import de.tilman_neumann.jml.smooth.CANEntry;
import de.tilman_neumann.jml.smooth.CANIterator;
import de.tilman_neumann.jml.transcendental.EulerConstant;
import de.tilman_neumann.jml.transcendental.Exp;
import de.tilman_neumann.jml.transcendental.Ln;
import de.tilman_neumann.jml.HarmonicNumbers;
Expand All @@ -39,7 +38,7 @@
import static de.tilman_neumann.jml.base.BigDecimalConstants.F_0;

/**
* Tests Robins and Lagarias' Riemann hypothesis tests on colossally abundant numbers (CANs).
* Tests Lagarias' Riemann hypothesis on colossally abundant numbers (CANs).
*
* From page 4 of http://www.math.lsa.umich.edu/~lagarias/doc/elementaryrh.pdf:
* "Robin showed that, if the Riemann hypothesis is false, then there will necessarily exist a counterexample
Expand All @@ -49,65 +48,12 @@
*
* Inequality (1.2) (Robin): sigma(n) <= exp(gamma) n ln(ln(n)) for each n >=5041
*/
public class RiemannHypothesisTest {
private static final Logger LOG = LogManager.getLogger(RiemannHypothesisTest.class);
public class RiemannLagariasHypothesisAnalyzer {
private static final Logger LOG = LogManager.getLogger(RiemannLagariasHypothesisAnalyzer.class);

private static final boolean DEBUG = false;

private static final Timer timer = new Timer();

/**
* Test inequality (1.2) with CANs.
*
* >24k CANs checked. Typical timings:
* Tested CAN(24003) with 118278 digits... t0=2297, t1=16, t2=0, t3=0, t4=16, t5=281
* Tested CAN(24004) with 118284 digits... t0=0, t1=26, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24005) with 118289 digits... t0=1735, t1=15, t2=0, t3=0, t4=16, t5=281
* Tested CAN(24006) with 118295 digits... t0=0, t1=18, t2=0, t3=0, t4=0, t5=313
* Tested CAN(24007) with 118300 digits... t0=1187, t1=31, t2=0, t3=0, t4=0, t5=282
* Tested CAN(24008) with 118306 digits... t0=0, t1=18, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24009) with 118311 digits... t0=1703, t1=32, t2=0, t3=0, t4=0, t5=296
* Tested CAN(24010) with 118314 digits... t0=0, t1=30, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24011) with 118319 digits... t0=609, t1=16, t2=0, t3=0, t4=15, t5=282
* Tested CAN(24012) with 118325 digits... t0=0, t1=24, t2=0, t3=0, t4=0, t5=297
*/
private static void runRobinsRHTest() {
long t0, t1, t2, t3, t4;

Scale scale = Scale.valueOf(30); // precision in after-floating point decimal digits
BigDecimal expGamma = Exp.exp(EulerConstant.gamma(scale), scale);

CANIterator canIter = new CANIterator();
for (int m=1; ; m++) {
timer.capture();
CANEntry canEntry = canIter.next();
BigInteger n = canEntry.getCAN();
t0 = timer.capture();

BigDecimal n_flt = new BigDecimal(n, 0);
BigDecimal lnln_n = Ln.ln(Ln.ln(n_flt, scale), scale);
t1 = timer.capture();

BigDecimal robin = expGamma.multiply(n_flt).multiply(lnln_n);
t2 = timer.capture();

// Fast sumOfDivisors computation with known prime factorization
SortedMultiset<BigInteger> factors = toSortedMultiset(canEntry.getPrimes(), canEntry.getExponents());
t3 = timer.capture();
if (DEBUG) LOG.debug("factors(n)=" + factors);
BigInteger sigma = Divisors.sumOfDivisors(factors);
BigDecimal diff = BigDecimalMath.subtract(robin, sigma);
t4 = timer.capture();
if (DEBUG) LOG.debug("sigma(n)=" + sigma + ", robin=" + robin + ", diff=" + diff);
if (diff.compareTo(F_0) < 0) {
LOG.info("Found RH counterexample candidate!");
LOG.info(" m=" + m + ": n has " + Magnitude.of(n) + " digits");
LOG.info(" n=" + n);
} else {
LOG.info("Tested CAN(" + m + ") with " + Magnitude.of(n) + " digits... t0=" + t0 + ", t1=" + t1 + ", t2=" + t2 + ", t3=" + t3 + ", t4=" + t4);
}
}
}

/**
* Test inequality (1.1) with CANs.
Expand Down Expand Up @@ -190,6 +136,5 @@ private static SortedMultiset<BigInteger> toSortedMultiset(ArrayList<BigInteger>
public static void main(String[] argv) {
ConfigUtil.initProject();
runLagariasRHTest();
//runRobinsRHTest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.primes;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

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

import de.tilman_neumann.jml.Divisors;
import de.tilman_neumann.jml.base.BigDecimalMath;
import de.tilman_neumann.jml.precision.Magnitude;
import de.tilman_neumann.jml.precision.Scale;
import de.tilman_neumann.jml.smooth.CANEntry;
import de.tilman_neumann.jml.smooth.CANIterator;
import de.tilman_neumann.jml.transcendental.EulerConstant;
import de.tilman_neumann.jml.transcendental.Exp;
import de.tilman_neumann.jml.transcendental.Ln;
import de.tilman_neumann.util.ConfigUtil;
import de.tilman_neumann.util.SortedMultiset;
import de.tilman_neumann.util.SortedMultiset_BottomUp;
import de.tilman_neumann.util.Timer;

import static de.tilman_neumann.jml.base.BigDecimalConstants.F_0;

/**
* Tests Robin's Riemann hypothesis tests on colossally abundant numbers (CANs).
*
* From page 4 of http://www.math.lsa.umich.edu/~lagarias/doc/elementaryrh.pdf:
* "Robin showed that, if the Riemann hypothesis is false, then there will necessarily exist a counterexample
* to the inequality (1.2) that is a colossally abundant number; the same property can be established for inequality (1.1)"
*
* Inequality (1.1) (Lagarias): sigma(n) <= Hn + exp(Hn)*ln(Hn)
*
* Inequality (1.2) (Robin): sigma(n) <= exp(gamma) n ln(ln(n)) for each n >=5041
*/
public class RiemannRobinHypothesisAnalyzer {
private static final Logger LOG = LogManager.getLogger(RiemannRobinHypothesisAnalyzer.class);

private static final boolean DEBUG = false;

private static final Timer timer = new Timer();

/**
* Test inequality (1.2) with CANs.
*
* >24k CANs checked. Typical timings:
* Tested CAN(24003) with 118278 digits... t0=2297, t1=16, t2=0, t3=0, t4=16, t5=281
* Tested CAN(24004) with 118284 digits... t0=0, t1=26, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24005) with 118289 digits... t0=1735, t1=15, t2=0, t3=0, t4=16, t5=281
* Tested CAN(24006) with 118295 digits... t0=0, t1=18, t2=0, t3=0, t4=0, t5=313
* Tested CAN(24007) with 118300 digits... t0=1187, t1=31, t2=0, t3=0, t4=0, t5=282
* Tested CAN(24008) with 118306 digits... t0=0, t1=18, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24009) with 118311 digits... t0=1703, t1=32, t2=0, t3=0, t4=0, t5=296
* Tested CAN(24010) with 118314 digits... t0=0, t1=30, t2=0, t3=0, t4=0, t5=297
* Tested CAN(24011) with 118319 digits... t0=609, t1=16, t2=0, t3=0, t4=15, t5=282
* Tested CAN(24012) with 118325 digits... t0=0, t1=24, t2=0, t3=0, t4=0, t5=297
*/
private static void runRobinsRHTest() {
long t0, t1, t2, t3, t4;

Scale scale = Scale.valueOf(30); // precision in after-floating point decimal digits
BigDecimal expGamma = Exp.exp(EulerConstant.gamma(scale), scale);

CANIterator canIter = new CANIterator();
for (int m=1; ; m++) {
timer.capture();
CANEntry canEntry = canIter.next();
BigInteger n = canEntry.getCAN();
t0 = timer.capture();

BigDecimal n_flt = new BigDecimal(n, 0);
BigDecimal lnln_n = Ln.ln(Ln.ln(n_flt, scale), scale);
t1 = timer.capture();

BigDecimal robin = expGamma.multiply(n_flt).multiply(lnln_n);
t2 = timer.capture();

// Fast sumOfDivisors computation with known prime factorization
SortedMultiset<BigInteger> factors = toSortedMultiset(canEntry.getPrimes(), canEntry.getExponents());
t3 = timer.capture();
if (DEBUG) LOG.debug("factors(n)=" + factors);
BigInteger sigma = Divisors.sumOfDivisors(factors);
BigDecimal diff = BigDecimalMath.subtract(robin, sigma);
t4 = timer.capture();
if (DEBUG) LOG.debug("sigma(n)=" + sigma + ", robin=" + robin + ", diff=" + diff);
if (diff.compareTo(F_0) < 0) {
LOG.info("Found RH counterexample candidate!");
LOG.info(" m=" + m + ": n has " + Magnitude.of(n) + " digits");
LOG.info(" n=" + n);
} else {
LOG.info("Tested CAN(" + m + ") with " + Magnitude.of(n) + " digits... t0=" + t0 + ", t1=" + t1 + ", t2=" + t2 + ", t3=" + t3 + ", t4=" + t4);
}
}
}

private static SortedMultiset<BigInteger> toSortedMultiset(ArrayList<BigInteger> primes, ArrayList<Integer> exponents) {
SortedMultiset<BigInteger> factors = new SortedMultiset_BottomUp<>();
for (int i=0; i<primes.size(); i++) {
factors.add(primes.get(i), exponents.get(i));
}
return factors;
}

public static void main(String[] argv) {
ConfigUtil.initProject();
runRobinsRHTest();
}
}

0 comments on commit bb7ac05

Please sign in to comment.