From c8b37f6c2be811bfbc3cef6d67bb54dca9db6c56 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Sat, 15 Nov 2025 12:53:46 +0530 Subject: [PATCH 1/3] Optimize power() using exponentiation by squaring (O(log n)) - Vinayak --- .../maths/PowerUsingRecursion.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java index 93c8252ab929..37c1781850c8 100644 --- a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java +++ b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java @@ -2,21 +2,44 @@ /** * calculate Power using Recursion - * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + * @author Vinayak (https://github.com/Vinayak-v12) */ public final class PowerUsingRecursion { private PowerUsingRecursion() { } + /** + * Computes base raised to the given exponent. + * + * @param base the number to be raised + * @param exponent the power (can be negative) + * @return base^exponent + */ + public static double power(double base, int exponent) { - // Base case: anything raised to the power of 0 is 1 + + // Handle negative exponent: a^-n = 1 / (a^n) + if (exponent < 0) { + return 1.0 / power(base, -exponent); + } + + // Base cases if (exponent == 0) { - return 1; + return 1.0; + } + if (exponent == 1) { + return base; + } + + // Exponentiation by Squaring + // If exponent is even: a^n = (a^(n/2))^2 + if (exponent % 2 == 0) { + double half = power(base, exponent / 2); + return half * half; } - // Recursive case: base ^ exponent = base * base ^ (exponent - 1) - // Recurse with a smaller exponent and multiply with base + // If exponent is odd: a^n = a * a^(n-1) return base * power(base, exponent - 1); } } From b617dff8a8caa6f6879c6ce6393559b26c396949 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Sat, 15 Nov 2025 13:29:28 +0530 Subject: [PATCH 2/3] Add edge case tests for power() to increase coverage --- .../maths/PowerUsingRecursionTest.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java index 705cc6672818..e82df90b08dd 100644 --- a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java @@ -6,15 +6,24 @@ /** * Test case for Power using Recursion - * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + * @author Vinayak (https://github.com/Vinayak-v12) */ class PowerUsingRecursionTest { @Test void testPowerUsingRecursion() { - assertEquals(32.0, PowerUsingRecursion.power(2.0, 5)); - assertEquals(97.65625, PowerUsingRecursion.power(2.5, 5)); - assertEquals(81, PowerUsingRecursion.power(3, 4)); + // exponent = 0 + assertEquals(1.0, PowerUsingRecursion.power(5.0, 0)); + + // exponent = 1 + assertEquals(5.0, PowerUsingRecursion.power(5.0, 1)); + + // negative exponent + assertEquals(0.25, PowerUsingRecursion.power(2.0, -2)); + + // another negative exponent + assertEquals(0.5, PowerUsingRecursion.power(2.0, -1)); } + } From 053744205f1c2fe469dcdce3460668beb1ceec88 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Sat, 15 Nov 2025 14:09:03 +0530 Subject: [PATCH 3/3] Fix formatting issues reported by clang-format --- src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java | 1 - .../java/com/thealgorithms/maths/PowerUsingRecursionTest.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java index 37c1781850c8..e87038483ff2 100644 --- a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java +++ b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java @@ -16,7 +16,6 @@ private PowerUsingRecursion() { * @param exponent the power (can be negative) * @return base^exponent */ - public static double power(double base, int exponent) { // Handle negative exponent: a^-n = 1 / (a^n) diff --git a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java index e82df90b08dd..16f779eb965f 100644 --- a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java @@ -25,5 +25,4 @@ void testPowerUsingRecursion() { // another negative exponent assertEquals(0.5, PowerUsingRecursion.power(2.0, -1)); } - }