From 2b5653ba5de686df7c9774ad98ba9424042fef42 Mon Sep 17 00:00:00 2001 From: Devin Lucas <115660983+dlucas489@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:05:29 -0700 Subject: [PATCH] Use Math.multiplyExact for safe multiplication in pow method Replaced multiplication with Math.multiplyExact for overflow checks. --- src/main/java/com/thealgorithms/maths/Pow.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/Pow.java b/src/main/java/com/thealgorithms/maths/Pow.java index 377b37ac4569..55c3f7f7fa31 100644 --- a/src/main/java/com/thealgorithms/maths/Pow.java +++ b/src/main/java/com/thealgorithms/maths/Pow.java @@ -22,6 +22,7 @@ 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}{@code b} as a {@code long}. * @throws IllegalArgumentException if {@code b} is negative. + * @throws ArithmeticException if result isn't representable as a long. */ public static long pow(int a, int b) { if (b < 0) { @@ -29,7 +30,7 @@ public static long pow(int a, int b) { } long result = 1; for (int i = 1; i <= b; i++) { - result *= a; + result = Math.multiplyExact(result, a); } return result; }