From 2e398c70bfbd6eb5b070ec5a8b0998fdb8fe8453 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 28 Aug 2023 07:24:39 +0530 Subject: [PATCH 1/3] modified Power using recursion code --- .../com/thealgorithms/maths/PowRecursion.java | 23 ------------------- .../maths/PowerUsingRecursion.java | 20 ++++++++++++++++ .../maths/PowerUsingRecursionTest.java | 20 ++++++++++++++++ 3 files changed, 40 insertions(+), 23 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/maths/PowRecursion.java create mode 100644 src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java create mode 100644 src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java diff --git a/src/main/java/com/thealgorithms/maths/PowRecursion.java b/src/main/java/com/thealgorithms/maths/PowRecursion.java deleted file mode 100644 index e8241424c6bd..000000000000 --- a/src/main/java/com/thealgorithms/maths/PowRecursion.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.thealgorithms.maths; - -public class PowRecursion { - - public static void main(String[] args) { - assert Double.compare(pow(2, 0), Math.pow(2, 0)) == 0; - assert Double.compare(pow(0, 2), Math.pow(0, 2)) == 0; - assert Double.compare(pow(2, 10), Math.pow(2, 10)) == 0; - assert Double.compare(pow(10, 2), Math.pow(10, 2)) == 0; - } - - /** - * Returns the value of the first argument raised to the power of the second - * argument - * - * @param a the base. - * @param b the exponent. - * @return the value {@code a}{@code b}. - */ - public static long pow(int a, int b) { - return b == 0 ? 1 : a * pow(a, b - 1); - } -} diff --git a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java new file mode 100644 index 000000000000..65648ecfc4fc --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java @@ -0,0 +1,20 @@ +package com.thealgorithms.maths; + +/** + * calculate Power using Recursion + * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + */ + +public class PowerUsingRecursion { + + public static double power(double base, int exponent) { + // Base case: anything raised to the power of 0 is 1 + if (exponent == 0) { + return 1; + } + + // Recursive case: base ^ exponent = base * base ^ (exponent - 1) + // Recurse with a smaller exponent and multiply with base + return base * power(base, exponent - 1); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java new file mode 100644 index 000000000000..445266108af2 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java @@ -0,0 +1,20 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +/** + * Test case for Power using Recursion + * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + */ + +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)); + } +} \ No newline at end of file From 3966e97a85504144630b7f214c2a644bea66938d Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 28 Aug 2023 07:29:48 +0530 Subject: [PATCH 2/3] solved linter --- .../com/thealgorithms/maths/PowerUsingRecursionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java index 445266108af2..a6cd67b02385 100644 --- a/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java +++ b/src/test/java/com/thealgorithms/maths/PowerUsingRecursionTest.java @@ -13,8 +13,8 @@ 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)); + assertEquals(32.0, PowerUsingRecursion.power(2.0, 5)); + assertEquals(97.65625, PowerUsingRecursion.power(2.5, 5)); + assertEquals(81, PowerUsingRecursion.power(3, 4)); } } \ No newline at end of file From dee64c030013fa3e3a923c3341c8382c78ed1420 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 28 Aug 2023 07:57:20 +0530 Subject: [PATCH 3/3] add white space --- src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java index 65648ecfc4fc..4d547c25412c 100644 --- a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java +++ b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java @@ -12,7 +12,7 @@ public static double power(double base, int exponent) { if (exponent == 0) { return 1; } - + // Recursive case: base ^ exponent = base * base ^ (exponent - 1) // Recurse with a smaller exponent and multiply with base return base * power(base, exponent - 1);