Skip to content

Commit 2fb1dcb

Browse files
committed
feat: add Factorial algorithm using recursion
1 parent aa8be9f commit 2fb1dcb

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.recursion;
22

33
/*
4-
The Factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
5-
It is defined as:
6-
n! = n × (n - 1) × (n - 2) × ... × 1
7-
with the base case:
8-
0! = 1
4+
The Factorial of a non-negative integer n is the product of all positive integers less than or
5+
equal to n. It is defined as: n! = n × (n - 1) × (n - 2) × ... × 1 with the base case: 0! = 1
96
107
Example:
118
5! = 5 × 4 × 3 × 2 × 1 = 120
129
*/
1310

1411
public final class Factorial {
12+
private Factorial() {
13+
throw new UnsupportedOperationException("Utility class");
14+
}
1515

16-
private Factorial() {
17-
throw new UnsupportedOperationException("Utility class");
16+
/**
17+
* Computes the factorial of a non-negative integer using recursion.
18+
*
19+
* @param n the number for which factorial is to be calculated
20+
* @return factorial of n
21+
* @throws IllegalArgumentException if n is negative
22+
*/
23+
public static long factorial(int n) {
24+
if (n < 0) {
25+
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
1826
}
19-
20-
/**
21-
* Computes the factorial of a non-negative integer using recursion.
22-
*
23-
* @param n the number for which factorial is to be calculated
24-
* @return factorial of n
25-
* @throws IllegalArgumentException if n is negative
26-
*/
27-
public static long factorial(int n) {
28-
if (n < 0) {
29-
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
30-
}
31-
if (n == 0 || n == 1) {
32-
return 1;
33-
}
34-
return n * factorial(n - 1);
27+
if (n == 0 || n == 1) {
28+
return 1;
3529
}
30+
return n * factorial(n - 1);
31+
}
3632
}

src/test/java/com/thealgorithms/recursion/FactorialTest.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@
66
import org.junit.jupiter.api.Test;
77

88
public class FactorialTest {
9-
10-
@Test
11-
public void testFactorialOfZero() {
12-
assertEquals(1, Factorial.factorial(0));
13-
}
14-
15-
@Test
16-
public void testFactorialOfOne() {
17-
assertEquals(1, Factorial.factorial(1));
18-
}
19-
20-
@Test
21-
public void testFactorialOfPositiveNumbers() {
22-
assertEquals(120, Factorial.factorial(5));
23-
assertEquals(720, Factorial.factorial(6));
24-
assertEquals(5040, Factorial.factorial(7));
25-
}
26-
27-
@Test
28-
public void testFactorialOfTen() {
29-
assertEquals(3628800, Factorial.factorial(10));
30-
}
31-
32-
@Test
33-
public void testNegativeNumberThrowsException() {
34-
assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
35-
}
9+
@Test
10+
public void testFactorialOfZero() {
11+
assertEquals(1, Factorial.factorial(0));
12+
}
13+
14+
@Test
15+
public void testFactorialOfOne() {
16+
assertEquals(1, Factorial.factorial(1));
17+
}
18+
19+
@Test
20+
public void testFactorialOfPositiveNumbers() {
21+
assertEquals(120, Factorial.factorial(5));
22+
assertEquals(720, Factorial.factorial(6));
23+
assertEquals(5040, Factorial.factorial(7));
24+
}
25+
26+
@Test
27+
public void testFactorialOfTen() {
28+
assertEquals(3628800, Factorial.factorial(10));
29+
}
30+
31+
@Test
32+
public void testNegativeNumberThrowsException() {
33+
assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
34+
}
3635
}

0 commit comments

Comments
 (0)