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 01/10] 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 02/10] 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 f2a9960a8dac69f9d22b8f055bbc8a0ee0ecc377 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:11:20 +0000 Subject: [PATCH 03/10] fix: add missing newline at end of file for Checkstyle --- src/test/java/com/thealgorithms/recursion/FactorialTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 228cbf7c2ff4..4455653fa779 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -33,4 +33,4 @@ public void testFactorialOfTen() { public void testNegativeNumberThrowsException() { assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); } -} \ No newline at end of file +} From 27e3b12c1f00ad2c3f92931a5cd1c5c4a43bb3d2 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:22:05 +0000 Subject: [PATCH 04/10] cling update --- .../thealgorithms/recursion/Factorial.java | 43 +++++++-------- .../recursion/FactorialTest.java | 53 +++++++++---------- 2 files changed, 46 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..c193c0c9d79e 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -1,36 +1,33 @@ 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 + */ - /** - * 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); + 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); + } } diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 4455653fa779..e76dfba509d7 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)); + } } From f64a46a9fda6179c4b76af27dc21db94c63727a2 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:35:54 +0000 Subject: [PATCH 05/10] fix: add newline at end of files for Checkstyle --- src/main/java/com/thealgorithms/recursion/Factorial.java | 1 + src/test/java/com/thealgorithms/recursion/FactorialTest.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java index c193c0c9d79e..1840ac3554bd 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -31,3 +31,4 @@ public static long factorial(int n) { 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 e76dfba509d7..916def360aa5 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -33,3 +33,4 @@ public void testNegativeNumberThrowsException() { assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); } } + From 35be17e240c881ef4bd8616c1ae28843a593d887 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:50:25 +0000 Subject: [PATCH 06/10] Fix code formatting according to Clang-format --- .../thealgorithms/recursion/Factorial.java | 6 ++--- .../recursion/FactorialTest.java | 23 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java index 1840ac3554bd..4238ad7d7366 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -20,15 +20,15 @@ private Factorial() { * @return factorial of n * @throws IllegalArgumentException if n is negative */ - - public static long factorial(int n) { + + 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); } } - diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 916def360aa5..ef43ef1b2402 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 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)); } } - From e4ea56379d8108e9cb5d318d9e10bf47a65588bc Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:55:08 +0000 Subject: [PATCH 07/10] Remove trailing spaces and ensure newline at EOF --- src/main/java/com/thealgorithms/recursion/Factorial.java | 2 +- .../java/com/thealgorithms/recursion/FactorialTest.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java index 4238ad7d7366..e8fe1f363082 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -20,7 +20,7 @@ private Factorial() { * @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."); diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index ef43ef1b2402..fce61de77b17 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -10,24 +10,24 @@ public class FactorialTest { 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)); From f029c33e15b04cacb055447ee5f40340581ef143 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:03:26 +0000 Subject: [PATCH 08/10] Fix formatting issues for clang-format compliance --- .../thealgorithms/recursion/Factorial.java | 47 +++++++------------ .../recursion/FactorialTest.java | 40 +++++++--------- 2 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Factorial.java b/src/main/java/com/thealgorithms/recursion/Factorial.java index e8fe1f363082..5144469743d3 100644 --- a/src/main/java/com/thealgorithms/recursion/Factorial.java +++ b/src/main/java/com/thealgorithms/recursion/Factorial.java @@ -1,34 +1,23 @@ -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."); + private Factorial() { + throw new UnsupportedOperationException("Utility class"); } - if (n == 0 || n == 1) { - return 1; + /** + * 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); } - 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 fce61de77b17..56fda122dd63 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -1,35 +1,27 @@ -package com.thealgorithms.recursion; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - +import static org.junit.jupiter.api.Assertions.*; 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 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 testFactorialOfTen() { + assertEquals(3628800, Factorial.factorial(10)); + } - @Test - public void testNegativeNumberThrowsException() { - assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); - } + @Test + public void testNegativeNumberThrowsException() { + assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); + } } From 7a7f7b6b2cdbfe69a860cd32aacbf6c222727ed7 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:06:21 +0000 Subject: [PATCH 09/10] Add blank line between static and normal imports for clang-format --- src/test/java/com/thealgorithms/recursion/FactorialTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 56fda122dd63..7b5497f18d26 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -1,4 +1,5 @@ import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; public class FactorialTest { From 335b6530847f9f9c921a575fe10ae900bf4954f1 Mon Sep 17 00:00:00 2001 From: Ayush Raj <74952106+Ayush41@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:13:10 +0000 Subject: [PATCH 10/10] Replace wildcard import with explicit JUnit imports --- src/test/java/com/thealgorithms/recursion/FactorialTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/recursion/FactorialTest.java b/src/test/java/com/thealgorithms/recursion/FactorialTest.java index 7b5497f18d26..f5783f7c57a7 100644 --- a/src/test/java/com/thealgorithms/recursion/FactorialTest.java +++ b/src/test/java/com/thealgorithms/recursion/FactorialTest.java @@ -1,4 +1,5 @@ -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test;