Repository navigation
Compute a large factorial as a balanced product, and keep only the small ones - #2329
Merged
Rafael-SOWNet merged 1 commit intoNov 2, 2026
Merged
Conversation
…all ones Every factorial up to the largest one asked for was kept, each made from the one before by multiplying the whole running product by one more factor. (10^5)! took 21 s, allocated 12.9 GB, and left 8.6 GB on the heap after a full collection, for the rest of the process. Factorials up to 1000! are still cached (half a megabyte in all). Past that the factorial is a balanced product tree in System.Numerics.BigInteger, which multiplies large numbers subquadratically, and is not kept: | | before | after | |---|---|---| | (10^5)! mod 1000003 | 21 s, 13.7 GB | 0.11 s, 8.8 MB | | 20000!, allocated | 276 MB | 1.2 MB | | heap after (10^5)!, after a full GC | 8.6 GB | 3 MB | The same tree in EInteger took 0.44 s and 453 MB for 1 2 ... 10^5; BigInteger takes 0.1 s and 7 MB, and the conversion back is 2 ms. #2327 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 2, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2329 +/- ##
=======================================
Coverage 86.75% 86.75%
=======================================
Files 312 312
Lines 68447 68456 +9
Branches 19909 19912 +3
=======================================
+ Hits 59379 59390 +11
+ Misses 4908 4907 -1
+ Partials 4160 4159 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Rafael-SOWNet
deleted the
a-large-factorial-is-a-product-tree-and-is-not-kept
branch
November 2, 2026 13:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following up Happypig375's comment on #2327 about the speed and memory of large modulo operations. The first cost there was the factorial itself.
Every factorial up to the largest one asked for was cached, each built from the one before by multiplying the whole running product by one more factor. Now:
System.Numerics.BigInteger, and it is not kept.(10^5)! mod 100000320000!, allocated(10^5)!and a full GCThe same tree in
EIntegertakes 0.44 s and 453 MB for1 · 2 ⋯ 10^5; inBigIntegerit takes 0.1 s and 7 MB, and the conversion back takes 2 ms. No answer changes.Tests:
ALargeFactorialTestchecks factorials on both sides of the cache bound against an independent product, and checks(10^5)! mod 1000003against Python. It also asserts that20000!allocates under 20 MB; that fails on master, at 306 MB.A remainder of a large power or factorial is still computed from the whole number. A separate PR follows for that.
🤖 Generated with Claude Code