-
Notifications
You must be signed in to change notification settings - Fork 28
Add script to calc min overlap of primes #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This PR adds a new analysis script to compute a minimum-sum set of n distinct primes whose overlap (LCM/product) meets a given threshold, with a CLI for running the search from the command line.
Changes:
- Added
analysis/min_prime_overlap/min_overlap_primes.pyimplementing a branch-and-bound search for the minimum-sum prime set meeting an LCM/product threshold. - Implemented primality testing (trial division + Miller–Rabin), prime enumeration helpers, and pruning/bounding utilities.
- Added an argparse-based CLI to run the computation and print the resulting primes, sum, and overlap.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def int_nth_root_ceil(a: int, n: int) -> int: | ||
| """Smallest integer x such that x**n >= a (a>=0, n>=1).""" | ||
| if n < 1: | ||
| raise ValueError("n must be >= 1") | ||
| if a <= 1: | ||
| return 1 |
Copilot
AI
Feb 8, 2026
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.
int_nth_root_ceil’s docstring says it supports a>=0, but for a==0 it returns 1 (the smallest x with x**n >= 0 is 0). Either handle a==0 explicitly or tighten the documented precondition to a>=1 to avoid incorrect reuse.
| - Exact solution via branch-and-bound. | ||
| - Uses deterministic Miller-Rabin for 64-bit integers (and a reasonable fallback for bigger). |
Copilot
AI
Feb 8, 2026
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.
The module docstring claims an “Exact solution via branch-and-bound”, but is_probable_prime falls back to a non-deterministic Miller–Rabin base set for n >= 2**64, which can admit pseudoprimes and break exactness for very large thresholds. Consider either making primality deterministic for all supported sizes, or clarifying the exactness claim/limits in the docstring.
| - Exact solution via branch-and-bound. | |
| - Uses deterministic Miller-Rabin for 64-bit integers (and a reasonable fallback for bigger). | |
| - Branch-and-bound search that is mathematically exact given correct primality tests. | |
| - Uses a deterministic Miller-Rabin base set for 64-bit integers; for larger integers it | |
| falls back to a probabilistic test, so for astronomically large thresholds the result is | |
| extremely high-confidence but not formally guaranteed to be exact. |
| @lru_cache(maxsize=None) | ||
| def next_prime_ge(n: int) -> int: | ||
| """Return the smallest prime >= n.""" |
Copilot
AI
Feb 8, 2026
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.
@lru_cache(maxsize=None) makes next_prime_ge’s cache unbounded. For large thresholds/search spaces, this can grow without limit and cause high memory usage. Consider using a bounded maxsize (and/or making caching configurable) to reduce OOM risk.
| @lru_cache(maxsize=None) | ||
| def sum_smallest_primes_ge(start: int, count: int) -> int: | ||
| """Sum of the 'count' smallest distinct primes >= start.""" | ||
| s = 0 |
Copilot
AI
Feb 8, 2026
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.
sum_smallest_primes_ge is cached with maxsize=None (unbounded). Depending on how many distinct (start, count) pairs the DFS explores, this can grow very large in-memory. Consider bounding the cache or computing this bound without caching unboundedly.
| @lru_cache(maxsize=None) | ||
| def list_smallest_primes_ge(start: int, count: int) -> Tuple[int, ...]: | ||
| """Tuple of the 'count' smallest distinct primes >= start.""" | ||
| res: List[int] = [] |
Copilot
AI
Feb 8, 2026
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.
list_smallest_primes_ge is cached with maxsize=None (unbounded) and returns tuples whose total stored size can become large. For long searches this may significantly increase memory usage; consider a bounded cache or alternative bound computation.
| @@ -0,0 +1,258 @@ | |||
|
|
|||
Copilot
AI
Feb 8, 2026
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.
The file starts with a blank line before the shebang. That prevents the OS from recognizing the #!/usr/bin/env python3 interpreter directive when running the script directly. Remove the leading blank line so the shebang is the first line of the file.
This pull request introduces a new script,
min_overlap_primes.py, which provides an efficient solution for finding a set ofndistinct primes whose least common multiple (LCM) meets or exceeds a specified threshold, while minimizing the sum of the primes. The script uses advanced techniques such as branch-and-bound search, efficient primality testing, and mathematical optimizations to ensure both correctness and performance.Key features and improvements:
Prime selection and optimization:
ndistinct primes with the minimum possible sum such that their LCM is at least (or strictly greater than) a given thresholdt, with early pruning for efficiency.Primality testing and utilities:
Command-line interface: