Repository navigation
feat(pref sums): add p50 #54
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| def sieve(n: int): | ||
| is_prime = [True] * (n + 1) | ||
| if n >= 0: | ||
| is_prime[0] = False | ||
| if n >= 1: | ||
| is_prime[1] = False | ||
| 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 | ||
|
|
||
| def consecutive_prime_sum(n: int) -> Optional[int]: | ||
| is_prime, primes = sieve(n) | ||
| longest_sum = 0 | ||
| longest_sum_prime = None | ||
| pref_sums = [0]*(len(primes)+1) | ||
| # go through every prefix sum (pairs of start and end primes), sum should be prime | ||
| for i in range(1, len(pref_sums)): | ||
| pref_sums[i] = pref_sums[i-1] + primes[i-1] | ||
|
|
||
| for start in range(len(primes)): | ||
| for end in range(start+longest_sum+1, len(primes)+1): | ||
| s = pref_sums[end]-pref_sums[start] | ||
| if s >= n: | ||
| break | ||
| if is_prime[s]: | ||
| longest_sum = end-start | ||
| longest_sum_prime = s | ||
| return longest_sum_prime | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from Algorithms.numerical_methods.euler.p50 import consecutive_prime_sum, sieve | ||
|
|
||
|
|
||
| def test_sieve_marks_zero_and_one_as_non_prime(): | ||
| is_prime, primes = sieve(10) | ||
| assert is_prime[0] is False | ||
| assert is_prime[1] is False | ||
| assert primes == [2, 3, 5, 7] | ||
|
|
||
|
|
||
| def test_sol(): | ||
| assert consecutive_prime_sum(n=1_000_000) == 997651 | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.