Skip to content

Conversation

@trishajp38-source
Copy link

Summary

This PR improves the existing Factorial implementation in the divideandconquer package by replacing the recursive algorithm with an optimized iterative version. The new implementation reduces space complexity, avoids recursion overhead, and prevents stack overflow for moderately large inputs.

In addition, this PR adds a comprehensive JUnit test suite to validate correctness, edge cases, performance behavior, and relative improvement compared to the original recursive approach.


✔ Algorithm Improvements

  • Replaced recursive factorial implementation with iterative approach
  • Eliminated O(n) recursive stack usage → now O(1) space complexity
  • Improved execution time by removing recursion overhead
  • Fully maintains expected mathematical behavior for all valid inputs
  • Safer for moderately large values (e.g., 15!, 20!) compared to recursion

✔ Test Suite Added (FactorialTest.java)

The added JUnit test suite covers:

1. Basic Correctness

  • factorial(5) → 120
  • factorial(0) → 1
  • Negative inputs throw IllegalArgumentException

2. Performance & Analysis

  • Benchmarks factorial(15)
  • Validates timing constraints and expected performance characteristics

3. Demonstrating Algorithmic Improvement

  • Contains a local copy of the original recursive method for comparison
  • Shows iterative version is faster in practice
  • Confirms iterative algorithm handles larger inputs safely without stack overflow

Why This PR Is Valuable

  • Improves algorithmic quality and safety
  • Adds missing test coverage for educational clarity
  • Matches project philosophy of readable, practical algorithm implementations
  • Clean, minimal changes limited to relevant files only
  • Helps future learners compare recursion vs iteration in factorial computation

Thank you for reviewing this contribution!

@codecov-commenter
Copy link

codecov-commenter commented Nov 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.56%. Comparing base (a9ba87c) to head (b7942fb).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7145   +/-   ##
=========================================
  Coverage     78.56%   78.56%           
+ Complexity     6794     6791    -3     
=========================================
  Files           762      761    -1     
  Lines         22520    22517    -3     
  Branches       4429     4428    -1     
=========================================
- Hits          17693    17691    -2     
  Misses         4115     4115           
+ Partials        712      711    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@trishajp38-source
Copy link
Author

All CI checks have passed.
Formatting has been applied using clang-format.
Please review the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants