Skip to content

Commit

Permalink
clean up for jml methods in Primality
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Dec 26, 2020
1 parent d8be1f1 commit 641bb53
Showing 1 changed file with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import de.tilman_neumann.jml.factor.CombinedFactorAlgorithm;
import de.tilman_neumann.util.SortedMultiset;
import de.tilman_neumann.util.SortedMultiset_BottomUp;
import it.unimi.dsi.fastutil.ints.Int2IntMap;

/**
Expand All @@ -36,7 +37,7 @@
*/
public class Primality {

private static class PrimePowerTreedMap extends TreeMap<BigInteger, Integer> {
private static class PrimePowerTreedMap extends SortedMultiset_BottomUp<BigInteger> {
private static final long serialVersionUID = 7802239809732541730L;

@Override
Expand All @@ -50,7 +51,7 @@ public Integer put(BigInteger key, Integer value) {
}
}

private static class SquareFreeTreedMap extends TreeMap<BigInteger, Integer> {
private static class SquareFreeTreedMap extends SortedMultiset_BottomUp<BigInteger> {
private static final long serialVersionUID = -7769218967264615452L;

@Override
Expand Down Expand Up @@ -753,7 +754,7 @@ public static List<BigInteger> factorize(final BigInteger val, List<BigInteger>
* @param n
* @param map of all BigInteger primes and their associated exponents
*/
public static void factorInteger(final BigInteger n, SortedMap<BigInteger, Integer> map) {
public static void factorInteger(final BigInteger n, SortedMultiset<BigInteger> map) {
Map<Integer, Integer> tMap = new TreeMap<Integer, Integer>();
BigInteger N = countPrimes1021(n, tMap);
if (tMap.size() > 0) {
Expand All @@ -769,11 +770,7 @@ public static void factorInteger(final BigInteger n, SortedMap<BigInteger, Integ
} else {
factorizer = new CombinedFactorAlgorithm(1, null, false, false, true);
}
SortedMultiset<BigInteger> set = factorizer.factor(N);

for (Map.Entry<BigInteger, Integer> entry : set.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
factorizer.factor(N, map);
}
return;
}
Expand All @@ -786,17 +783,14 @@ public static void factorInteger(final BigInteger n, SortedMap<BigInteger, Integ
* @return map of all BigInteger primes and their associated exponents
*/
public static SortedMap<BigInteger, Integer> factorInteger(final BigInteger n) {
SortedMap<BigInteger, Integer> map = new TreeMap<BigInteger, Integer>();
factorInteger(n, map);
return map;
// CombinedFactorAlgorithm factorizer;
// if (Config.JAVA_UNSAFE) {
// final int cores = Runtime.getRuntime().availableProcessors();
// factorizer = new CombinedFactorAlgorithm(cores / 2 + 1, null, true, false, true);
// } else {
// factorizer = new CombinedFactorAlgorithm(1, null, false, false, true);
// }
// return factorizer.factor(n);
CombinedFactorAlgorithm factorizer;
if (Config.JAVA_UNSAFE) {
final int cores = Runtime.getRuntime().availableProcessors();
factorizer = new CombinedFactorAlgorithm(cores / 2 + 1, null, true, false, true);
} else {
factorizer = new CombinedFactorAlgorithm(1, null, false, false, true);
}
return factorizer.factor(n);
}

public static BigInteger rho(final BigInteger val) {
Expand Down Expand Up @@ -1061,7 +1055,7 @@ public static int moebiusMu(BigInteger value) {
if (value.equals(BigInteger.ONE)) {
return 1;
}
SortedMap<BigInteger, Integer> map = new SquareFreeTreedMap();
SortedMultiset<BigInteger> map = new SquareFreeTreedMap();
try {
factorInteger(value, map);
// value is square-free
Expand Down Expand Up @@ -1147,7 +1141,7 @@ public static boolean isPrimePower(BigInteger value) {
return false;
}
try {
SortedMap<BigInteger, Integer> map = new PrimePowerTreedMap();
PrimePowerTreedMap map = new PrimePowerTreedMap();
factorInteger(value, map);
if (map.size() == 1) {
for (Map.Entry<BigInteger, Integer> entry : map.entrySet()) {
Expand Down Expand Up @@ -1200,9 +1194,8 @@ public static boolean isSquareFree(BigInteger val) {
if (val.compareTo(BigInteger.ZERO) < 0) {
val = val.negate();
}
SortedMap<BigInteger, Integer> map = new SquareFreeTreedMap();
try {
factorInteger(val, map);
factorInteger(val, new SquareFreeTreedMap());
return true;
} catch (ReturnException re) {
}
Expand Down

0 comments on commit 641bb53

Please sign in to comment.