Skip to content

Commit 5e8d61c

Browse files
committed
Library.java: Tweaked some code and comments for clarity.
1 parent ab495df commit 5e8d61c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

java/Library.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static int sqrt(int x) {
3434
if (x < 0)
3535
throw new IllegalArgumentException("Square root of negative number");
3636
int y = 0;
37-
for (int i = 32768; i != 0; i >>>= 1) {
37+
for (int i = 1 << 15; i != 0; i >>>= 1) {
3838
y |= i;
3939
if (y > 46340 || y * y > x)
4040
y ^= i;
@@ -109,19 +109,19 @@ public static int powMod(int x, int y, int m) {
109109

110110
// Exponentiation by squaring
111111
int z = 1;
112-
while (y != 0) {
112+
for (; y != 0; y >>>= 1) {
113113
if ((y & 1) != 0)
114114
z = (int)((long)z * x % m);
115115
x = (int)((long)x * x % m);
116-
y >>>= 1;
117116
}
118117
return z;
119118
}
120119

121120

122-
// Returns x^-1 mod m, where the result is in the range [0, m). Note that (x * x^-1) mod m = (x^-1 * x) mod m = 1.
121+
// Returns x^-1 mod m, where the result is in the range [0, m).
122+
// Note that (x * x^-1) mod m = (x^-1 * x) mod m = 1.
123123
public static int reciprocalMod(int x, int m) {
124-
if (!(m > 0 && 0 <= x && x < m))
124+
if (!(0 <= x && x < m))
125125
throw new IllegalArgumentException();
126126

127127
// Based on a simplification of the extended Euclidean algorithm
@@ -179,7 +179,7 @@ public static int gcd(int x, int y) {
179179
}
180180

181181

182-
// Tests whether the given integer is prime.
182+
// Tests whether the given non-negative integer is prime.
183183
public static boolean isPrime(int x) {
184184
if (x < 0)
185185
throw new IllegalArgumentException("Negative number");
@@ -201,7 +201,8 @@ else if (x == 2)
201201

202202
// Returns a Boolean array 'isPrime' where isPrime[i] indicates whether i is prime, for 0 <= i <= n.
203203
// For a large batch of queries, this is faster than calling isPrime() for each integer.
204-
// For example: listPrimality(100) = {false, false, true, true, false, true, false, true, false, false, ...} (array length 101).
204+
// For example: listPrimality(100) = {false, false, true, true, false, true, false, true,
205+
// false, false, false, true, false, true, false, false, false, true, ...} (array length 101).
205206
public static boolean[] listPrimality(int n) {
206207
if (n < 0)
207208
throw new IllegalArgumentException("Negative array size");
@@ -243,7 +244,7 @@ public static int[] listPrimes(int n) {
243244
}
244245

245246

246-
// Returns an array spf where spf[k] is the smallest prime factor of k, valid for 0 <= k <= n.
247+
// Returns an array spf where spf[k] is the smallest prime factor of k, valid for 2 <= k <= n.
247248
// For example: listSmallestPrimeFactors(10) = {0, 0, 2, 3, 2, 5, 2, 7, 2, 3, 2}.
248249
public static int[] listSmallestPrimeFactors(int n) {
249250
int[] result = new int[n + 1];

0 commit comments

Comments
 (0)