Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 28 additions & 4 deletions src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
package com.thealgorithms.maths;

/**
* calculate Power using Recursion
* Utility class to calculate power using recursion.
*
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/

public final class PowerUsingRecursion {
private PowerUsingRecursion() {
}

/**
* Calculates the power of a base raised to an exponent using recursion.
*
* @param base the base value
* @param exponent the power to raise the base to
* @return the result of base^exponent
* @throws ArithmeticException if base is 0 and exponent is negative
*/
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
// Handle negative exponents: base^(-e) = 1 / (base^e)
if (exponent < 0) {
if (base == 0.0) {
throw new ArithmeticException("Base cannot be 0 with a negative exponent.");
}
if (exponent == Integer.MIN_VALUE) {
return 1.0 / base * power(base, -(exponent + 1));
}
return 1.0 / power(base, -exponent);
}

// Recursive case: divide-and-conquer for even exponents to reduce depth to O(log n)
if (exponent % 2 == 0) {
double half = power(base, exponent / 2);
return half * half;
}

// Recursive case for odd exponents
return base * power(base, exponent - 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Test case for Power using Recursion
* Test cases for Power using Recursion.
*
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/

class PowerUsingRecursionTest {

@ParameterizedTest
@CsvSource({"2.0, 5, 32.0", "2.5, 5, 97.65625", "3.0, 4, 81.0", "5.0, 0, 1.0", "-2.0, 3, -8.0", "-2.0, 4, 16.0", "2.0, -1, 0.5", "2.0, -3, 0.125", "-2.0, -1, -0.5", "-2.0, -2, 0.25", "0.0, 0, 1.0", "0.0, 5, 0.0", "1.0, -5, 1.0", "1.0, 100000, 1.0", "-1.0, 100000, 1.0", "-1.0, 100001, -1.0"})
void testPower(double base, int exponent, double expected) {
assertEquals(expected, PowerUsingRecursion.power(base, exponent), 1e-9);
}

@Test
void testZeroBaseWithNegativeExponentThrowsException() {
assertThrows(ArithmeticException.class, () -> PowerUsingRecursion.power(0.0, -1));
assertThrows(ArithmeticException.class, () -> PowerUsingRecursion.power(0.0, -10));
}

@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));
void testIntegerMinValueExponent() {
assertEquals(1.0, PowerUsingRecursion.power(1.0, Integer.MIN_VALUE), 1e-9);
assertEquals(1.0, PowerUsingRecursion.power(-1.0, Integer.MIN_VALUE), 1e-9);
}
}
Loading