Skip to content

Commit 532d4b0

Browse files
author
alxkm
committed
refactor: Pow
1 parent 74b05ef commit 532d4b0

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

src/main/java/com/thealgorithms/maths/Pow.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
package com.thealgorithms.maths;
22

3-
// POWER (exponentials) Examples (a^b)
3+
/**
4+
* A utility class for computing exponentiation (power) of integers.
5+
* <p>
6+
* This class provides a method to calculate the value of a base raised to a given exponent using a simple iterative approach.
7+
* For example, given a base {@code a} and an exponent {@code b}, the class computes {@code a}<sup>{@code b}</sup>.
8+
* </p>
9+
*/
410
public final class Pow {
511
private Pow() {
612
}
713

8-
public static void main(String[] args) {
9-
assert pow(2, 0) == Math.pow(2, 0); // == 1
10-
assert pow(0, 2) == Math.pow(0, 2); // == 0
11-
assert pow(2, 10) == Math.pow(2, 10); // == 1024
12-
assert pow(10, 2) == Math.pow(10, 2); // == 100
13-
}
14-
1514
/**
16-
* Returns the value of the first argument raised to the power of the second
17-
* argument
15+
* Computes the value of the base raised to the power of the exponent.
16+
* <p>
17+
* The method calculates {@code a}<sup>{@code b}</sup> by iteratively multiplying the base {@code a} with itself {@code b} times.
18+
* If the exponent {@code b} is negative, an {@code IllegalArgumentException} is thrown.
19+
* </p>
1820
*
19-
* @param a the base.
20-
* @param b the exponent.
21-
* @return the value {@code a}<sup>{@code b}</sup>.
21+
* @param a the base of the exponentiation. Must be a non-negative integer.
22+
* @param b the exponent to which the base {@code a} is raised. Must be a non-negative integer.
23+
* @return the result of {@code a}<sup>{@code b}</sup> as a {@code long}.
24+
* @throws IllegalArgumentException if {@code b} is negative.
2225
*/
2326
public static long pow(int a, int b) {
27+
if (b < 0) {
28+
throw new IllegalArgumentException("Exponent must be non-negative.");
29+
}
2430
long result = 1;
2531
for (int i = 1; i <= b; i++) {
2632
result *= a;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
public class PowTest {
11+
@ParameterizedTest
12+
@CsvSource({"2, 0, 1", "0, 2, 0", "2, 10, 1024", "10, 2, 100", "5, 3, 125", "3, 4, 81"})
13+
void testPow(int base, int exponent, long expected) {
14+
assertEquals(expected, Pow.pow(base, exponent), "Failed for base: " + base + " and exponent: " + exponent);
15+
}
16+
17+
@Test
18+
void testPowThrowsExceptionForNegativeExponent() {
19+
assertThrows(IllegalArgumentException.class, () -> Pow.pow(2, -1));
20+
}
21+
22+
@Test
23+
void testPowHandlesLargeNumbers() {
24+
assertEquals(1048576, Pow.pow(2, 20));
25+
}
26+
27+
@Test
28+
void testPowHandlesZeroBase() {
29+
assertEquals(0, Pow.pow(0, 5));
30+
}
31+
32+
@Test
33+
void testPowHandlesOneBase() {
34+
assertEquals(1, Pow.pow(1, 100));
35+
}
36+
}

0 commit comments

Comments
 (0)