Repository navigation
Take a remainder of a whole power or factorial as it is built - #2330
Merged
Rafael-SOWNet merged 1 commit intoNov 2, 2026
Merged
Rafael-SOWNet merged 1 commit into
Rafael-SOWNet merged 1 commit into
Conversation
b ^ e mod m and n! mod m, for whole b, e >= 0, n >= 0 and m not 0, wrote the power or the factorial out first and then divided. They are now reduced modulo m as they are built: the power by modular exponentiation, the factorial factor by factor (in machine words under 2^31), and n! mod m is 0 outright once n >= |m|, since |m| then divides it. The remainder is the floored one, as the operator's. | | before | after | |---|---|---| | 2^(10^8) mod 1000 | 29 s, 13.9 GB | 2 ms, 0.8 MB | | 2^(10^7) mod 1000 | 0.68 s, 772 MB | 44 ms, 0.8 MB | | 10^(10^9) mod 7 | no answer in 60 s | 35 ms | | (10^5)! mod 1000003 | 21 s, 13.7 GB | 4 ms, 0.8 MB | | (10^7)! mod 1000000007 | -- | 62 ms | 0 ^ 0, a negative or symbolic exponent, and a divisor that is not a whole number are left to the evaluation as before. No answer changes; the ones that change are those that never finished. #2327 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2330 +/- ##
=======================================
Coverage 86.75% 86.75%
=======================================
Files 312 312
Lines 68447 68477 +30
Branches 19909 19921 +12
=======================================
+ Hits 59379 59406 +27
- Misses 4908 4913 +5
+ Partials 4160 4158 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Member
|
This shows the importance of separating numerical evaluation from all symbolic operations in v3, there are a lot of special forms that we can take advantage of that would result in huge numbers if evaluated directly |
Member
Author
|
Agreed. Here it's a shortcut in |
Rafael-SOWNet
deleted the
a-remainder-of-a-power-or-factorial-is-taken-as-it-is-built
branch
November 2, 2026 14:28
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.
The second half of the follow-up to Happypig375's comment on #2327, after #2329.
Before this PR,
b ^ e mod mandn! mod m, for whole numbers, wrote out the whole power or factorial and then divided. Now they are reduced modulomas they are built:|m| < 2^31;n! mod mis 0 outright oncen >= |m|, since|m|then divides it.The remainder is the floored one, as for the operator.
2^(10^8) mod 10002^(10^7) mod 100010^(10^9) mod 7(10^5)! mod 10000030 ^ 0, a negative or symbolic exponent, and a divisor that is not a whole number go to the evaluation as before. No answer changes, apart from the ones that never finished.Tests:
ARemainderOfALargeWholeTestchecks 16 remainders against Python'spowandmath.factorial, including negative bases and divisors,0 ^ 0and a reciprocal. It also asserts that2 ^ (10 ^ 7) mod 1000allocates under 20 MB; that fails on master, at 810 MB.🤖 Generated with Claude Code