You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Project Euler p50 (consecutive prime sum) using sieve + prefix sums
✨ Enhancement🧪 Tests🕐 10-20 Minutes
AI Description
• Add Euler p50 solver to find longest consecutive-prime sum below a limit.
• Use sieve-generated primality table plus prime prefix sums for fast range-sum checks.
• Add pytest coverage asserting the known 1,000,000-limit result.
The following are alternative approaches to this PR:
1. Extract shared prime utilities module
➕ Removes duplicated is_prime/sieve logic across Euler solutions
➕ Centralizes correctness/performance fixes and test coverage
➖ Adds an extra abstraction layer for what are currently stand-alone puzzle solutions
➖ Requires updating imports across multiple existing files
2. Use a two-pointer/sliding-window over prefix sums
➕ Can reduce search space vs nested loops by exploiting monotonicity of sums
➕ Often faster for larger limits while preserving the prefix-sum approach
➖ More complex control flow and harder to reason about correctness
➖ Less straightforward to keep the “longest length” invariant correct
3. Leverage an external number theory library (e.g., sympy)
➕ Battle-tested primality and prime generation
➕ Potentially simpler code
➖ Introduces a heavy new dependency for a small utility repo
➖ May conflict with repository goals around minimal dependencies/learning implementations
Recommendation: The current sieve + prefix-sums approach is the right baseline for Euler p50 (simple, fast enough at 1e6, easy to test). If this repo continues adding many prime-based Euler problems, the most valuable follow-up is extracting shared prime utilities to avoid repeated implementations and to centralize performance/correctness improvements.
Files changed (2) +49 / -0
Enhancement (1) +45 / -0
p50.pyImplement Euler p50 solver with sieve + prime prefix sums+45/-0
Implement Euler p50 solver with sieve + prime prefix sums
• Adds primality helpers (trial-division is_prime and sieve) and implements consecutive_prime_sum(n) using a prime list plus prefix sums to check all candidate consecutive-prime ranges under n while tracking the longest valid prime sum.
+from typing import Optional++def is_prime(n: int):+ if n < 1:+ raise ValueError("n must be at least 1")+ elif n == 1:+ return False+ elif n == 2:+ return True+ elif n % 2 == 0:+ return False+ else:+ for i in range(3, int(math.sqrt(n))+1):+ if n % i == 0:+ return False+ return True
Relevance
⭐⭐⭐ High
PR #53’s similar is_prime uses math.sqrt and explicitly imports math; team likely fixes missing
imports.
ⓘ Recommendations generated based on similar findings in past PRs
Evidence
In p50.py, math.sqrt is referenced inside is_prime() but there is no import math in the module
imports. In p49.py, the same pattern includes import math, showing the missing import in p50.py is
not intentional.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
### Issue description
`Algorithms/numerical_methods/euler/p50.py` defines `is_prime()` that calls `math.sqrt(...)` but the module never imports `math`, causing a `NameError` when `is_prime()` is executed.
### Issue Context
A similar `is_prime()` implementation in `p49.py` imports `math` at module scope.
### Fix Focus Areas
- Algorithms/numerical_methods/euler/p50.py[1-16]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
sieve() initializes the returned primality array to all True and never sets indices 0 and 1 to
False, so the returned lookup table incorrectly reports 0 and 1 as prime.
+def sieve(n: int):+ is_prime = [True]*(n+1)+ primes = []+ for p in range(2, n+1):+ if is_prime[p]:+ primes.append(p)+ for v in range(p*p, n+1, p):+ is_prime[v] = False+ return is_prime, primes
Relevance
⭐⭐ Medium
No repo history found about sieve() needing is_prime[0]=is_prime[1]=False; enforcement unclear.
ⓘ Recommendations generated based on similar findings in past PRs
Evidence
p50.py returns the boolean array directly without ever correcting 0/1. Another sieve implementation
in the repo explicitly sets indices 0 and 1 to False, demonstrating the expected invariant for such
tables.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
### Issue description
`sieve(n)` returns a boolean primality table but leaves `is_prime[0]` and `is_prime[1]` as `True`, which is incorrect and can break any caller that relies on the returned table.
### Issue Context
There is an existing sieve implementation in the repo that explicitly sets 0 and 1 to non-prime.
### Fix Focus Areas
- Algorithms/numerical_methods/euler/p50.py[18-26]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
ⓘ Recommendations generated based on similar findings in past PRs
Evidence
The added test file content ends at line 4 (no trailing newline). A similar missing-newline issue
was previously accepted as a fix in this repo, indicating this is a recurring style/maintenance
concern.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
### Issue description
The test file does not end with a newline character.
### Issue Context
This repo has previously accepted fixes to add trailing newlines to avoid repeated diffs on unchanged lines.
### Fix Focus Areas
- tests/euler/p50_test.py[1-4]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
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
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.
No description provided.