From dd077d34664f2de2663e3754c6115582a249f9cb Mon Sep 17 00:00:00 2001 From: Prasad Pitke <120767959+Prasad-Pitke@users.noreply.github.com> Date: Thu, 12 Nov 2026 09:07:25 +0530 Subject: [PATCH] Use Math.multiplyExact for overflow handling in pow --- 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..33dda5ba2ddc 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 the result overflows a {@code 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, (long) a); } return result; }