Skip to content

Commit 2aca2ad

Browse files
committed
extract core method
1 parent 08962fc commit 2aca2ad

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

src/main/java/de/tilman_neumann/jml/modular/ModularPower.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ public class ModularPower {
5252
* @return a^b (mod c)
5353
*/
5454
public int modPow(BigInteger a, long b, int c) {
55-
// products need long precision
56-
long modPow = 1;
57-
long aModC = a.mod(BigInteger.valueOf(c)).longValue();
58-
while (b > 0) {
59-
if ((b&1) == 1) modPow = (modPow * aModC) % c;
60-
aModC = (aModC * aModC) % c;
61-
b >>= 1;
62-
}
63-
return (int) modPow;
55+
return modPowCore(a.mod(BigInteger.valueOf(c)).longValue(), b, c);
6456
}
6557

6658
/**
@@ -71,9 +63,11 @@ public int modPow(BigInteger a, long b, int c) {
7163
* @return a^b (mod c)
7264
*/
7365
public int modPow(int a, long b, int c) {
74-
// products need long precision
66+
return modPowCore(a % c, b, c);
67+
}
68+
69+
private int modPowCore(long aModC, long b, long c) {
7570
long modPow = 1;
76-
long aModC = a % c;
7771
while (b > 0) {
7872
if ((b&1) == 1) modPow = (modPow * aModC) % c;
7973
aModC = (aModC * aModC) % c;

0 commit comments

Comments
 (0)