-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix overflow when initializing a Sieve with T::MAX #47
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like here we're branching on something that will never happen unless the private attributes are messed with? Also, instead of returning
None
for a composite now the method returnsNone
for a composite or if there's an overflow, which can be confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overflow can actually happen without any malicious shenanigans (and is guarded against with the
sieve_with_max_start
test).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you about the odd control flow here though. It gets complex quick:
base
toT::MAX
(like the test).next()
update_residues()
will work fine becauseincr
== 0 (returns true)last_round
to true as a side effect.maybe_next()
which will also work fine becauseT::MAX
is composite (returns None)incr
andincr_limit
are 2 as a side effect.update_residues
again, and given thatincr
is now 2 and less or equal toincr_limit
, we match on the first check and returntrue
(note:last_round
is true, which would cause returning false)maybe_next
again but now the addition overflows so we returnNone
and the main loopcontinue
supdate_residues()
for the third time, we finally hit the check forlast_round
, so we return falsewhile
-loop innext()
to exit and we returnNone
Pheew!!!
Maybe the Sieve constructor should check if the given
start
is worth the effort? E.g. check ifT::MAX - start
contains "enough" primes. Maybe using PNT and check thatT::MAX/log(T::MAX) - start/log(start)
is larger than some number (1?, 10?)? Or can you think of something smarter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…then again, there are a lot of primes, so even if they pick a start that is 99.999999% of
T::MAX
there are still a LOT of prime candidates left, so maybe we just check thatstart != T::MAX
and that's good enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't particularly like the idea of heuristic checks and relying on probabilities in this specific case (the sieve), I think if it's possible to do exactly, we should try to do it.
Not necessarily, 2^128-1 and 2^8192-1 are primes. (Actually the former can be used as a test)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean 2^127 -1? Is that the last prime before U128::MAX, probably not right? Seems like a bit of an arbitrary test, or maybe I'm misunderstanding what you're suggesting here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my mistake. Any
2^n-1
is indeed composite ifn
is composite.