Use Chinese Remainder Theorem rather than sieving#82
Closed
ebblake wants to merge 1 commit into
Closed
Conversation
Sieving takes, on average, O(sum(factors)) iterations. Back in 2016 day 15, where there are only 7 primes all less than 100 (and their LCM still fits in 32 bits), this is fast. But for 2020 day 13, there are 9 primes with two over 400 (with LCM around 50 bits). That means sieving will take around 1000 divisions. Better is to just use CRT directly, implementing extended Euclidean division to compute coefficients, and modular multiplication to utilize u128 intermediates to avoid overflows, at which point the answer can be obtained with fewer hardware divisions. On my laptop, this speeds up part2 runtime from 3.6us to 0.4us.
maneatingape
reviewed
Jun 7, 2026
maneatingape
left a comment
Owner
There was a problem hiding this comment.
Solution is already < 1µs, so the extra complexity of the CRT is not worth the tradeoff.
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.
Description
Sieving takes, on average, O(sum(factors)) iterations. Back in 2016 day 15, where there are only 7 primes all less than 100 (and their LCM still fits in 32 bits), this is fast. But for 2020 day 13, there are 9 primes with two over 400 (with LCM around 50 bits). That means sieving will take around 1000 divisions. Better is to just use CRT directly, implementing extended Euclidean division to compute coefficients, and modular multiplication to utilize u128 intermediates to avoid overflows, at which point the answer can be obtained with fewer hardware divisions.
On my laptop, this speeds up part2 runtime from 3.6us to 0.4us.
Type of change
Checklist
using the same naming conventions. Code should be portable, avoiding any
architecture-specific intrinsics.
cargo testcargo fmt -- `find . -name "*.rs"`cargo clippy --all-targets --all-featuresFormatting and linting also can be executed by running
just(if installed) on the command line at the project root.