Skip to content

Commit 2203ccd

Browse files
authored
Use BigInteger with primitives instead of strings (williamfiset#396)
1 parent b25b0a3 commit 2203ccd

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/AnagramSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public AnagramSet(int[] mods) {
5353

5454
// // Assuming all mods are primes each mod value will have a modular inverse
5555
for (int i = 0; i < N_HASHES; i++) {
56-
java.math.BigInteger mod = new java.math.BigInteger(String.valueOf(MODS[i]));
56+
java.math.BigInteger mod = java.math.BigInteger.valueOf(MODS[i]);
5757
for (int j = 0; j < PRIMES.length; j++) {
58-
java.math.BigInteger prime = new java.math.BigInteger(String.valueOf(PRIMES[j]));
58+
java.math.BigInteger prime = java.math.BigInteger.valueOf(PRIMES[j]);
5959
MOD_INVERSES[i][j] = prime.modInverse(mod).intValue();
6060
}
6161
}

src/main/java/com/williamfiset/algorithms/datastructures/bloomfilter/StringSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public StringSet(int[] mods, int maxLen) {
5353
rollingHashes = new long[N_HASHES];
5454
bloomFilter = new BloomFilter(mods);
5555

56-
java.math.BigInteger bigAlpha = new java.math.BigInteger(String.valueOf(ALPHABET_SZ));
56+
java.math.BigInteger bigAlpha = java.math.BigInteger.valueOf(ALPHABET_SZ);
5757

5858
// Assuming all mods are primes each mod value will have a modular inverse
5959
for (int i = 0; i < N_HASHES; i++) {
60-
java.math.BigInteger mod = new java.math.BigInteger(String.valueOf(MODS[i]));
60+
java.math.BigInteger mod = java.math.BigInteger.valueOf(MODS[i]);
6161
MOD_INVERSES[i] = bigAlpha.modInverse(mod).intValue();
6262
}
6363

src/main/java/com/williamfiset/algorithms/datastructures/hashtable/HashTableDoubleHashing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected int probe(int x) {
4444
// probing so that all the cells can be reached.
4545
@Override
4646
protected void adjustCapacity() {
47-
while (!(new BigInteger(String.valueOf(capacity)).isProbablePrime(20))) {
47+
while (!(BigInteger.valueOf(capacity).isProbablePrime(20))) {
4848
capacity++;
4949
}
5050
}

src/main/java/com/williamfiset/algorithms/math/ModPow.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
package com.williamfiset.algorithms.math;
1313

14+
import java.math.BigInteger;
15+
1416
public class ModPow {
1517

1618
// The values placed into the modPow function cannot be greater
@@ -87,12 +89,12 @@ public static long modPow(long a, long n, long mod) {
8789
// Example usage
8890
public static void main(String[] args) {
8991

90-
java.math.BigInteger A, N, M, r1;
92+
BigInteger A, N, M, r1;
9193
long a, n, m, r2;
9294

93-
A = new java.math.BigInteger("3");
94-
N = new java.math.BigInteger("4");
95-
M = new java.math.BigInteger("1000000");
95+
A = BigInteger.valueOf(3);
96+
N = BigInteger.valueOf(4);
97+
M = BigInteger.valueOf(1000000);
9698
a = A.longValue();
9799
n = N.longValue();
98100
m = M.longValue();
@@ -102,9 +104,9 @@ public static void main(String[] args) {
102104
r2 = modPow(a, n, m); // 81
103105
System.out.println(r1 + " " + r2);
104106

105-
A = new java.math.BigInteger("-45");
106-
N = new java.math.BigInteger("12345");
107-
M = new java.math.BigInteger("987654321");
107+
A = BigInteger.valueOf(-45);
108+
N = BigInteger.valueOf(12345);
109+
M = BigInteger.valueOf(987654321);
108110
a = A.longValue();
109111
n = N.longValue();
110112
m = M.longValue();
@@ -114,9 +116,9 @@ public static void main(String[] args) {
114116
r2 = modPow(a, n, m); // 323182557
115117
System.out.println(r1 + " " + r2);
116118

117-
A = new java.math.BigInteger("6");
118-
N = new java.math.BigInteger("-66");
119-
M = new java.math.BigInteger("101");
119+
A = BigInteger.valueOf(6);
120+
N = BigInteger.valueOf(-66);
121+
M = BigInteger.valueOf(101);
120122
a = A.longValue();
121123
n = N.longValue();
122124
m = M.longValue();
@@ -126,9 +128,9 @@ public static void main(String[] args) {
126128
r2 = modPow(a, n, m); // 84
127129
System.out.println(r1 + " " + r2);
128130

129-
A = new java.math.BigInteger("-5");
130-
N = new java.math.BigInteger("-7");
131-
M = new java.math.BigInteger("1009");
131+
A = BigInteger.valueOf(-5);
132+
N = BigInteger.valueOf(-7);
133+
M = BigInteger.valueOf(1009);
132134
a = A.longValue();
133135
n = N.longValue();
134136
m = M.longValue();
@@ -139,9 +141,9 @@ public static void main(String[] args) {
139141
System.out.println(r1 + " " + r2);
140142

141143
for (int i = 0; i < 1000; i++) {
142-
A = new java.math.BigInteger(a + "");
143-
N = new java.math.BigInteger(n + "");
144-
M = new java.math.BigInteger(m + "");
144+
A = BigInteger.valueOf(a);
145+
N = BigInteger.valueOf(n);
146+
M = BigInteger.valueOf(m);
145147
a = Math.random() < 0.5 ? randLong(MAX) : -randLong(MAX);
146148
n = randLong();
147149
m = randLong(MAX);

src/main/java/com/williamfiset/algorithms/math/NChooseRModPrime.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ public static long compute(int N, int R, int P) {
3535
// Method for testing output against the output generated by the compute(int,int,int) function
3636
private static String bigIntegerNChooseRModP(int N, int R, int P) {
3737
if (R == 0) return "1";
38-
BigInteger num = new BigInteger("1");
39-
BigInteger den = new BigInteger("1");
38+
BigInteger num = BigInteger.ONE;
39+
BigInteger den = BigInteger.ONE;
4040
while (R > 0) {
41-
num = num.multiply(new BigInteger("" + N));
42-
den = den.multiply(new BigInteger("" + R));
41+
num = num.multiply(BigInteger.valueOf(N));
42+
den = den.multiply(BigInteger.valueOf(R));
4343
BigInteger gcd = num.gcd(den);
4444
num = num.divide(gcd);
4545
den = den.divide(gcd);
4646
N--;
4747
R--;
4848
}
4949
num = num.divide(den);
50-
num = num.mod(new BigInteger("" + P));
50+
num = num.mod(BigInteger.valueOf(P));
5151
return num.toString();
5252
}
5353

src/main/java/com/williamfiset/algorithms/strings/RabinKarp.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RabinKarp {
1717
// same value as ' ' since 0*95^0 = 0*95^0 + 0*95^1 + 0*95^2
1818
private static final long ALPHABET_BASE = 95 + 1;
1919
private static final long[] ALPHABET = new long[127];
20-
private static final BigInteger BIG_ALPHA = new BigInteger(String.valueOf(ALPHABET_BASE));
20+
private static final BigInteger BIG_ALPHA = BigInteger.valueOf(ALPHABET_BASE);
2121

2222
// More primes: 1009, 1013, 1019, 10007, 10009, 10037, 100003, 100019, 100043, 1000003, 1000033,
2323
// 1000037,
@@ -35,7 +35,7 @@ public class RabinKarp {
3535

3636
// Compute modular inverses for chosen mod values
3737
for (int i = 0; i < N_HASHES; i++) {
38-
java.math.BigInteger mod = new java.math.BigInteger(String.valueOf(MODS[i]));
38+
java.math.BigInteger mod = java.math.BigInteger.valueOf(MODS[i]);
3939
MOD_INVERSES[i] = BIG_ALPHA.modInverse(mod).longValue();
4040
BIG_MODS[i] = mod;
4141
}
@@ -83,7 +83,7 @@ public static List<Integer> rabinKarp(String text, String pattern) {
8383
long[] patternHash = computeHash(pattern);
8484
long[] rollingHash = computeHash(text.substring(0, PL));
8585

86-
final BigInteger BIG_PL = new BigInteger(String.valueOf(PL));
86+
final BigInteger BIG_PL = BigInteger.valueOf(PL);
8787
final long[] POWERS = new long[N_HASHES];
8888
for (int i = 0; i < N_HASHES; i++)
8989
POWERS[i] = BIG_ALPHA.modPow(BIG_PL, BIG_MODS[i]).longValue();
@@ -121,7 +121,7 @@ public static List<Integer> rabinKarpBackwards(String text, String pattern) {
121121
long[] patternHash = computeHash(pattern);
122122
long[] rollingHash = computeHash(text.substring(TL - PL, TL));
123123

124-
final BigInteger BIG_PL = new BigInteger(String.valueOf(PL));
124+
final BigInteger BIG_PL = BigInteger.valueOf(PL);
125125
final long[] POWERS = new long[N_HASHES];
126126
for (int i = 0; i < N_HASHES; i++)
127127
POWERS[i] = BIG_ALPHA.modPow(BIG_PL, BIG_MODS[i]).longValue();

0 commit comments

Comments
 (0)