@@ -6,11 +6,9 @@ class MillerRabinMethod {
6
6
/**
7
7
*
8
8
* Generate a random BigInteger between {@code min} and {@code max}, inclusive.
9
- * Random BigInteger implementation from
10
- * <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow
11
- * answer</a> Uses an offset on the
12
- * {@link #RandomInRangeFromZeroToPositive(BigInteger)} function to return a
13
- * random BigInteger in a range.
9
+ * Random BigInteger implementation from <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow answer</a>
10
+ * Uses an offset on the {@link #RandomInRangeFromZeroToPositive(BigInteger)}
11
+ * function to return a random BigInteger in a range.
14
12
*
15
13
* @param min lower bound
16
14
* @param max upper bound
@@ -36,9 +34,8 @@ public static BigInteger RandomInRange(BigInteger min, BigInteger max) {
36
34
/**
37
35
*
38
36
* Generate a random BigInteger smaller or equal to {@code max}. Random
39
- * BigInteger implementation from
40
- * <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow
41
- * answer</a> Returns a random BigInteger lower than {@code max} derived from a
37
+ * BigInteger implementation from <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow* answer</a>
38
+ * Returns a random BigInteger lower than {@code max} derived from a
42
39
* random byte array" />
43
40
*
44
41
* @param max upper bound
@@ -86,10 +83,11 @@ private static BigInteger RandomInRangeFromZeroToPositive(BigInteger max) {
86
83
87
84
/**
88
85
*
89
- * Miller-Rabin primality test Test if {@code number} could be prime using the
90
- * Miller-Rabin Primality Test with {@code round} rounds. A return value of
91
- * false means {@code number} is definitely composite, while true means it is
92
- * probably prime. The higher {@code round} is, the more accurate the test is.
86
+ * Miller-Rabin primality test
87
+ * Test if {@code number} could be prime using the Miller-Rabin primality test
88
+ * with {@code round} rounds. A return value of false means {@code number} is
89
+ * definitely composite, while true means it is probably prime.
90
+ * The higher {@code round} is, the more accurate the test is.
93
91
*
94
92
* @param number the number to be tested for primality
95
93
* @param rounds how many rounds to use in the test
@@ -106,8 +104,8 @@ public static Boolean MillerRabin(BigInteger number, int rounds) {
106
104
// Factor out the powers of 2 from {number - 1} and save the result
107
105
BigInteger d = number .subtract (BigInteger .valueOf (1 ));
108
106
int r = 0 ;
109
- while (d . mod ( BigInteger . valueOf ( 2 )). equals ( BigInteger . valueOf ( 0 )) ) {
110
- d = d .divide ( BigInteger . valueOf ( 2 ) );
107
+ while (( d . getLowestSetBit () & 1 ) == 0 ) {
108
+ d = d .shiftLeft ( 1 );
111
109
++r ;
112
110
}
113
111
0 commit comments