Skip to content

Commit cd2a499

Browse files
committed
improve TDiv.factor(), fix typo in comment
1 parent c0bad7d commit cd2a499

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,20 @@ public void searchFactors(FactorArguments args, FactorResult result) {
111111
if (exp > 0) {
112112
// At least one exact division has occurred; add to results
113113
addToMap(p_i_big, exp*Nexp, primeFactors);
114-
if (N.bitLength() < 63) {
115-
// Check if we are done
116-
long p_i_square = ((long)p_i) * p_i;
117-
if (p_i_square > N.longValue()) {
118-
if (DEBUG) LOG.debug("N=" + N + " < p^2 = " + p_i_square);
119-
// the remaining N is 1 or prime
120-
if (N.compareTo(I_1)>0) addToMap(N, Nexp, primeFactors);
121-
result.smallestPossibleFactor = p_i; // may be helpful in following factor algorithms
122-
return;
123-
}
114+
}
115+
116+
// for random composite N, it is much much faster to check the termination condition after each p;
117+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
118+
int pbits = 32-Integer.numberOfLeadingZeros(p_i);
119+
if (pbits<<1 >= N.bitLength()) {
120+
// Check if we are done
121+
long p_i_square = ((long)p_i) * p_i;
122+
if (p_i_square > N.longValue()) {
123+
if (DEBUG) LOG.debug("N=" + N + " < p^2 = " + p_i_square);
124+
// the remaining N is 1 or prime
125+
if (N.compareTo(I_1)>0) addToMap(N, Nexp, primeFactors);
126+
result.smallestPossibleFactor = p_i; // may be helpful in following factor algorithms
127+
return;
124128
}
125129
}
126130
}

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv31.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
5050
primeFactors.add(BigInteger.valueOf(p), exp);
5151
}
5252
// for random composite N, it is much much faster to check the termination condition after each p;
53-
// for semiprime N, it would be ~40% faster to do it only after sucessful divisions
53+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
5454
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
5555
if (N>1) primeFactors.add(BigInteger.valueOf(N));
5656
break;

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv31Barrett.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void factor(BigInteger Nbig, int Nexp, SortedMultiset<BigInteger> primeFa
9494
primeFactors.add(BigInteger.valueOf(p), exp*Nexp);
9595
}
9696
// for random composite N, it is much much faster to check the termination condition after each p;
97-
// for semiprime N, it would be ~40% faster to do it only after sucessful divisions
97+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
9898
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
9999
break;
100100
}

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv31Inverse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void factor(BigInteger Nbig, int Nexp, SortedMultiset<BigInteger> primeFa
8585
primeFactors.add(BigInteger.valueOf(p), exp*Nexp);
8686
}
8787
// for random composite N, it is much much faster to check the termination condition after each p;
88-
// for semiprime N, it would be ~40% faster to do it only after sucessful divisions
88+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
8989
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
9090
break;
9191
}

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv63.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
6565
primeFactors.add(BigInteger.valueOf(p), exp);
6666
}
6767
// for random composite N, it is much much faster to check the termination condition after each p;
68-
// for semiprime N, it would be ~40% faster to do it only after sucessful divisions
68+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
6969
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
7070
break;
7171
}

src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv63Inverse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
129129
primeFactors.add(BigInteger.valueOf(p), exp);
130130
}
131131
// for random composite N, it is much much faster to check the termination condition after each p;
132-
// for semiprime N, it would be ~40% faster to do it only after sucessful divisions
132+
// for semiprime N, it would be ~40% faster to do it only after successful divisions
133133
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
134134
break; // the remaining N is prime
135135
}

0 commit comments

Comments
 (0)