Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Optimize Factorial Algorithm (Iterative Implementation) and Add Comprehensive JUnit Test Suite#7145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
trishajp38-source wants to merge5 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromtrishajp38-source:improve-factorial

Conversation

@trishajp38-source
Copy link

Summary

This PR improves the existingFactorial implementation in thedivideandconquer 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 throwIllegalArgumentException

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 commentedNov 29, 2025
edited
Loading

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.56%. Comparing base (a9ba87c) to head (b7942fb).

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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@DenizAltunkapanDenizAltunkapanAwaiting requested review from DenizAltunkapanDenizAltunkapan is a code owner

@yanglbmeyanglbmeAwaiting requested review from yanglbmeyanglbme is a code owner

@alxkmalxkmAwaiting requested review from alxkmalxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@trishajp38-source@codecov-commenter

[8]ページ先頭

©2009-2025 Movatter.jp