Skip to content

Commit ccf2de0

Browse files
author
Koushik Sai
committed
Move recursive factorial to recursion package and remove duplicate
1 parent 6bbabd8 commit ccf2de0

File tree

4 files changed

+19
-88
lines changed

4 files changed

+19
-88
lines changed

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

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
package com.thealgorithms.recursion;
22

3-
/*
4-
* Factorial of a number n is the product of all positive integers less than
5-
* or equal to n.
6-
*
7-
* n! = n × (n - 1) × (n - 2) × ... × 1
8-
*
9-
* Examples:
10-
* 0! = 1
11-
* 1! = 1
12-
* 5! = 120
13-
*/
14-
153
public final class FactorialRecursion {
16-
174
private FactorialRecursion() {
18-
throw new UnsupportedOperationException("Utility class");
195
}
20-
216
/**
22-
* Computes the factorial of a non-negative integer using recursion.
7+
* Recursive FactorialRecursion Method
238
*
24-
* @param n the number whose factorial is to be computed
25-
* @return factorial of n
26-
* @throws IllegalArgumentException if n is negative
9+
* @param n The number to factorial
10+
* @return The factorial of the number
2711
*/
2812
public static long factorial(int n) {
2913
if (n < 0) {
30-
throw new IllegalArgumentException("Factorial is not defined for negative numbers");
31-
}
32-
if (n <= 1) {
33-
return 1;
14+
throw new IllegalArgumentException("number is negative");
3415
}
35-
return n * factorial(n - 1);
16+
return n == 0 || n == 1 ? 1 : n * factorial(n - 1);
3617
}
3718
}

src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package com.thealgorithms.recursion;
22

3+
import java.util.stream.Stream;
4+
35
import static org.junit.jupiter.api.Assertions.assertEquals;
46
import static org.junit.jupiter.api.Assertions.assertThrows;
5-
67
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
711

8-
class FactorialRecursionTest {
9-
10-
@Test
11-
void testFactorialOfZero() {
12-
assertEquals(1, FactorialRecursion.factorial(0));
13-
}
14-
15-
@Test
16-
void testFactorialOfOne() {
17-
assertEquals(1, FactorialRecursion.factorial(1));
18-
}
12+
import com.thealgorithms.recursion.FactorialRecursion;
1913

20-
@Test
21-
void testFactorialOfPositiveNumber() {
22-
assertEquals(120, FactorialRecursion.factorial(5));
14+
public class FactorialRecursionTest {
15+
@ParameterizedTest
16+
@MethodSource("inputStream")
17+
void testFactorialRecursion(long expected, int number) {
18+
assertEquals(expected, FactorialRecursion.factorial(number));
2319
}
2420

25-
@Test
26-
void testFactorialOfLargerNumber() {
27-
assertEquals(3628800, FactorialRecursion.factorial(10));
21+
private static Stream<Arguments> inputStream() {
22+
return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
2823
}
2924

3025
@Test
31-
void testFactorialOfNegativeNumber() {
26+
void testThrowsForNegativeInput() {
3227
assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
3328
}
3429
}

0 commit comments

Comments
 (0)