From 1c69e7fc1965d24cbfac9f97105c6106ab38f8a6 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:34:37 +0000 Subject: [PATCH 1/3] added factorial using recursion --- .../thealgorithms/recursion/Factorial.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/com/thealgorithms/recursion/Factorial.java diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java new file mode 100644 index 000000000000..a4e1dd4dc815 --- /dev/null +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -0,0 +1,36 @@ +package com.thealgorithms.recursion; + +/* + The Factorial of a non-negative integer n is the product of all positive integers less than or equal to n. + It is defined as: + n! = n × (n - 1) × (n - 2) × ... × 1 + with the base case: + 0! = 1 + + Example: + 5! = 5 × 4 × 3 × 2 × 1 = 120 +*/ + +public final class Factorial { + + private Factorial() { + throw new UnsupportedOperationException("Utility class"); + } + + /** + * Computes the factorial of a non-negative integer using recursion. + * + * @param n the number for which factorial is to be calculated + * @return factorial of n + * @throws IllegalArgumentException if n is negative + */ + public static long factorial(int n) { + if (n < 0) { + throw new IllegalArgumentException("Factorial is not defined for negative numbers."); + } + if (n == 0 || n == 1) { + return 1; + } + return n * factorial(n - 1); + } +} From aa8be9f1818fc6eff62036d91e5d18ef45143a87 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:37:40 +0000 Subject: [PATCH 2/3] factorail test --- .../recursion/FactorialTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/java/com/thealgorithms/recursion/FactorialTest.java diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java new file mode 100644 index 000000000000..228cbf7c2ff4 --- /dev/null +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -0,0 +1,36 @@ +package com.thealgorithms.recursion; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class FactorialTest { + + @Test + public void testFactorialOfZero() { + assertEquals(1, Factorial.factorial(0)); + } + + @Test + public void testFactorialOfOne() { + assertEquals(1, Factorial.factorial(1)); + } + + @Test + public void testFactorialOfPositiveNumbers() { + assertEquals(120, Factorial.factorial(5)); + assertEquals(720, Factorial.factorial(6)); + assertEquals(5040, Factorial.factorial(7)); + } + + @Test + public void testFactorialOfTen() { + assertEquals(3628800, Factorial.factorial(10)); + } + + @Test + public void testNegativeNumberThrowsException() { + assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); + } +} \ No newline at end of file From 2fb1dcb039f8f347a42885a3acdb0cbc08a8f43a Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:57:39 +0000 Subject: [PATCH 3/3] feat: add Factorial algorithm using recursion --- .../thealgorithms/recursion/Factorial.java | 42 +++++++-------- .../recursion/FactorialTest.java | 53 +++++++++---------- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java index a4e1dd4dc815..63ffcf1feb96 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -1,36 +1,32 @@ package com.thealgorithms.recursion; /* - The Factorial of a non-negative integer n is the product of all positive integers less than or equal to n. - It is defined as: - n! = n × (n - 1) × (n - 2) × ... × 1 - with the base case: - 0! = 1 + The Factorial of a non-negative integer n is the product of all positive integers less than or + equal to n. It is defined as: n! = n × (n - 1) × (n - 2) × ... × 1 with the base case: 0! = 1 Example: 5! = 5 × 4 × 3 × 2 × 1 = 120 */ public final class Factorial { + private Factorial() { + throw new UnsupportedOperationException("Utility class"); + } - private Factorial() { - throw new UnsupportedOperationException("Utility class"); + /** + * Computes the factorial of a non-negative integer using recursion. + * + * @param n the number for which factorial is to be calculated + * @return factorial of n + * @throws IllegalArgumentException if n is negative + */ + public static long factorial(int n) { + if (n < 0) { + throw new IllegalArgumentException("Factorial is not defined for negative numbers."); } - - /** - * Computes the factorial of a non-negative integer using recursion. - * - * @param n the number for which factorial is to be calculated - * @return factorial of n - * @throws IllegalArgumentException if n is negative - */ - public static long factorial(int n) { - if (n < 0) { - throw new IllegalArgumentException("Factorial is not defined for negative numbers."); - } - if (n == 0 || n == 1) { - return 1; - } - return n * factorial(n - 1); + if (n == 0 || n == 1) { + return 1; } + return n * factorial(n - 1); + } } diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 228cbf7c2ff4..ac69bcfba152 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -6,31 +6,30 @@ import org.junit.jupiter.api.Test; public class FactorialTest { - - @Test - public void testFactorialOfZero() { - assertEquals(1, Factorial.factorial(0)); - } - - @Test - public void testFactorialOfOne() { - assertEquals(1, Factorial.factorial(1)); - } - - @Test - public void testFactorialOfPositiveNumbers() { - assertEquals(120, Factorial.factorial(5)); - assertEquals(720, Factorial.factorial(6)); - assertEquals(5040, Factorial.factorial(7)); - } - - @Test - public void testFactorialOfTen() { - assertEquals(3628800, Factorial.factorial(10)); - } - - @Test - public void testNegativeNumberThrowsException() { - assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); - } + @Test + public void testFactorialOfZero() { + assertEquals(1, Factorial.factorial(0)); + } + + @Test + public void testFactorialOfOne() { + assertEquals(1, Factorial.factorial(1)); + } + + @Test + public void testFactorialOfPositiveNumbers() { + assertEquals(120, Factorial.factorial(5)); + assertEquals(720, Factorial.factorial(6)); + assertEquals(5040, Factorial.factorial(7)); + } + + @Test + public void testFactorialOfTen() { + assertEquals(3628800, Factorial.factorial(10)); + } + + @Test + public void testNegativeNumberThrowsException() { + assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); + } } \ No newline at end of file