-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added solution for Project Euler problem 77. #3132
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 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a26071d
Added solution for Project Euler problem 77. Fixes: 2695
fpringle b9fb17f
Update docstrings, doctest, type annotations and 0-padding in directo…
fpringle 28bb925
Implemented lru_cache, better type hints, more doctests for problem 77
fpringle 9b5237c
updating DIRECTORY.md
a17c562
Merge branch 'master' into problem_77
fpringle 307a5ef
updating DIRECTORY.md
d0fb837
Added solution for Project Euler problem 77. Fixes: 2695
fpringle 9e0b3ef
Update docstrings, doctest, type annotations and 0-padding in directo…
fpringle 7b43475
Implemented lru_cache, better type hints, more doctests for problem 77
fpringle d0477e9
better variable names
fpringle 0477fc6
Merge branch 'problem_77' of github.com:fpringle/Python into problem_77
fpringle fdc3e99
Merge branch 'master' into problem_77
fpringle 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
Empty file.
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,66 @@ | ||
""" | ||
Project Euler Problem 77: https://projecteuler.net/problem=77 | ||
|
||
It is possible to write ten as the sum of primes in exactly five different ways: | ||
|
||
7 + 3 | ||
5 + 5 | ||
5 + 3 + 2 | ||
3 + 3 + 2 + 2 | ||
2 + 2 + 2 + 2 + 2 | ||
|
||
What is the first value which can be written as the sum of primes in over | ||
five thousand different ways? | ||
""" | ||
|
||
primes = set(range(3, 100, 2)) | ||
primes.add(2) | ||
for i in range(3, 100, 2): | ||
if i not in primes: | ||
continue | ||
primes.difference_update(set(range(i * i, 100, i))) | ||
|
||
CACHE_PARTITION = {0: {1}} | ||
|
||
|
||
def partition(n: int) -> set: | ||
fpringle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Return a set of integers corresponding to unique prime partitions of n. | ||
The unique prime partitions can be represented as unique prime decompositions, | ||
e.g. (7+3) <-> 7*3 = 12, (3+3+2+2) = 3*3*2*2 = 36 | ||
>>> partition(10) | ||
{32, 36, 21, 25, 30} | ||
>>> partition(15) | ||
{192, 160, 105, 44, 112, 243, 180, 150, 216, 26, 125, 126} | ||
""" | ||
if n < 0: | ||
return set() | ||
if n in CACHE_PARTITION: | ||
return CACHE_PARTITION[n] | ||
|
||
ret = set() | ||
for prime in primes: | ||
if prime > n: | ||
continue | ||
for sub in partition(n - prime): | ||
ret.add(sub * prime) | ||
|
||
CACHE_PARTITION[n] = ret | ||
return ret | ||
|
||
|
||
def solution(m: int = 5000) -> int: | ||
""" | ||
Return the smallest integer that can be written as the sum of primes in over | ||
m unique ways. | ||
>>> solution(500) | ||
45 | ||
""" | ||
for n in range(1, 100): | ||
if len(partition(n)) > m: | ||
return n | ||
return 0 | ||
fpringle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
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.