Skip to content

feat(pref sums): add p50 - #54

Merged
dchou1618 merged 2 commits into
masterfrom
feature/p50
Jul 17, 2026
Merged

dchou1618 merged 2 commits into
masterfrom
feature/p50

Conversation

@dchou1618

Copy link
Copy Markdown
Owner

No description provided.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Add Project Euler p50 (consecutive prime sum) using sieve + prefix sums

✨ Enhancement 🧪 Tests 🕐 10-20 Minutes

Grey Divider

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.
Diagram

graph TD
  T["tests/euler/p50_test.py"] --> F["consecutive_prime_sum()"] --> R["Longest prime"]
  M["Algorithms/.../p50.py"] --> F --> P["Prefix sums"] --> C["Scan ranges"]
  M --> S["sieve()"] --> L["Prime table"]
Loading
High-Level Assessment

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.

Algorithms/numerical_methods/euler/p50.py

Tests (1) +4 / -0
p50_test.pyAdd regression test for Euler p50 known 1e6 result +4/-0

Add regression test for Euler p50 known 1e6 result

• Introduces a pytest that asserts consecutive_prime_sum(1_000_000) returns the known Project Euler answer (997651).

tests/euler/p50_test.py

ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan

@qodo-code-review

qodo-code-review Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Missing math import ✓ Resolved 🐞 Bug ≡ Correctness
Description
Algorithms.numerical_methods.euler.p50.is_prime uses math.sqrt without importing math, so calling
is_prime will raise NameError at runtime.
Code

Algorithms/numerical_methods/euler/p50.py[R1-16]

+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.

PR-#53

ⓘ 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.

Algorithms/numerical_methods/euler/p50.py[1-16]
Algorithms/numerical_methods/euler/p49.py[1-18]

Agent prompt
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



Remediation recommended

2. Sieve mislabels 0/1 ✓ Resolved 🐞 Bug ≡ Correctness
Description
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.
Code

Algorithms/numerical_methods/euler/p50.py[R18-26]

+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.

Algorithms/numerical_methods/euler/p50.py[18-26]
dataSecurity/RSAEncryption/primality.py[8-19]

Agent prompt
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



Informational

3. Missing EOF newline ✓ Resolved 🐞 Bug ⚙ Maintainability
Description
tests/euler/p50_test.py is missing a trailing newline at end-of-file, which creates unnecessary diff
noise and may trip formatting/lint checks.
Code

tests/euler/p50_test.py[R1-4]

+from Algorithms.numerical_methods.euler.p50 import consecutive_prime_sum
+
+def test_sol():
+    assert consecutive_prime_sum(n=1_000_000) == 997651
Relevance

⭐⭐⭐ High

Trailing EOF newline was previously accepted as a cleanup fix (PR #49).

PR-#49

ⓘ 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.

tests/euler/p50_test.py[1-4]
PR-#49

Agent prompt
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


Grey Divider

ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan

Qodo Logo

Comment thread Algorithms/numerical_methods/euler/p50.py Outdated
Comment thread Algorithms/numerical_methods/euler/p50.py
Comment thread tests/euler/p50_test.py Outdated
@dchou1618
dchou1618 merged commit 68f27a8 into master Jul 17, 2026
5 checks passed
@dchou1618
dchou1618 deleted the feature/p50 branch July 17, 2026 03:45
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