Skip to content

Use BigInteger to prevent overflow in factorial calculation#7358

Merged
DenizAltunkapan merged 8 commits intoTheAlgorithms:masterfrom
VanshSharma3:fix/factorial-biginteger-overflow
Apr 1, 2026
Merged

Use BigInteger to prevent overflow in factorial calculation#7358
DenizAltunkapan merged 8 commits intoTheAlgorithms:masterfrom
VanshSharma3:fix/factorial-biginteger-overflow

Conversation

@VanshSharma3
Copy link
Copy Markdown
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

Updated the Factorial.java implementation to use BigInteger instead of long.

Reason for change:
The previous implementation using long would overflow for any input $n > 20$. Since factorials grow exponentially, BigInteger is necessary to handle arbitrarily large results accurately without data loss.

Changes:

  • Changed return type to BigInteger.
  • Updated the iterative loop to use BigInteger.multiply().
  • Added a check for negative input to throw IllegalArgumentException.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.39%. Comparing base (635d54a) to head (848f3c6).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7358      +/-   ##
============================================
- Coverage     79.40%   79.39%   -0.01%     
- Complexity     7092     7093       +1     
============================================
  Files           792      792              
  Lines         23226    23226              
  Branches       4568     4568              
============================================
- Hits          18443    18441       -2     
- Misses         4046     4047       +1     
- Partials        737      738       +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.

@VanshSharma3 VanshSharma3 marked this pull request as ready for review April 1, 2026 11:38
@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) April 1, 2026 11:52
@DenizAltunkapan DenizAltunkapan merged commit 9729e56 into TheAlgorithms:master Apr 1, 2026
7 checks passed
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.

3 participants