Skip to content

Conversation

@klei22
Copy link
Collaborator

@klei22 klei22 commented Feb 8, 2026

This pull request introduces a new script, min_overlap_primes.py, which provides an efficient solution for finding a set of n distinct 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:

  • Implements a branch-and-bound algorithm to find the set of n distinct primes with the minimum possible sum such that their LCM is at least (or strictly greater than) a given threshold t, with early pruning for efficiency.
  • Uses mathematical optimizations (such as integer nth-root and sum of smallest primes) to quickly estimate bounds and prune unpromising search branches.

Primality testing and utilities:

  • Provides a robust, deterministic Miller-Rabin primality test for 64-bit integers, with a fallback for larger numbers, ensuring correctness in prime generation.
  • Includes utility functions for quickly finding the next prime, summing the smallest primes above a threshold, and related helpers, all with memoization for performance.

Command-line interface:

  • Adds a command-line interface that allows users to specify the number of primes, the overlap threshold

Copy link

Copilot AI left a 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.py implementing 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.

Comment on lines +91 to +96
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
Copy link

Copilot AI Feb 8, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines 16 to 17
- Exact solution via branch-and-bound.
- Uses deterministic Miller-Rabin for 64-bit integers (and a reasonable fallback for bigger).
Copy link

Copilot AI Feb 8, 2026

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.

Suggested change
- 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.

Copilot uses AI. Check for mistakes.
Comment on lines +74 to +76
@lru_cache(maxsize=None)
def next_prime_ge(n: int) -> int:
"""Return the smallest prime >= n."""
Copy link

Copilot AI Feb 8, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +111 to +114
@lru_cache(maxsize=None)
def sum_smallest_primes_ge(start: int, count: int) -> int:
"""Sum of the 'count' smallest distinct primes >= start."""
s = 0
Copy link

Copilot AI Feb 8, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +123 to +126
@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] = []
Copy link

Copilot AI Feb 8, 2026

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.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,258 @@

Copy link

Copilot AI Feb 8, 2026

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.

Suggested change

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant