Skip to content

Commit dc2e645

Browse files
authored
Add exception when y < 0
1 parent f7e4d9c commit dc2e645

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/main/java/com/thealgorithms/maths/ModularExponentiation.java

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public int modExp(int x, int y, int m) {
1212
if (m < 1) {
1313
throw new IllegalArgumentException("Modulus must be positive integer");
1414
}
15+
if (y < 0) {
16+
throw new IllegalArgumentException(
17+
"Exponent must be zero or positive integer");
18+
}
1519
if (x == 0 || m == 1) {
1620
return 0;
1721
}

0 commit comments

Comments
 (0)