Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/thealgorithms/maths/Pow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ private Pow() {
* @param b the exponent to which the base {@code a} is raised. Must be a non-negative integer.
* @return the result of {@code a}<sup>{@code b}</sup> as a {@code long}.
* @throws IllegalArgumentException if {@code b} is negative.
* @throws ArithmeticException if the result overflows a {@code long}.
*/
public static long pow(int a, int b) {
if (b < 0) {
throw new IllegalArgumentException("Exponent must be non-negative.");
}
long result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
result = Math.multiplyExact(result, (long) a);
}
return result;
}
Expand Down
Loading