Skip to content

Hacktoberfest: Project Euler Solution Problem 77 #2782

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
68 changes: 68 additions & 0 deletions project_euler/problem_77/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Prime summations
Problem 77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the reference link to the problem


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?
"""


def solution(length=5000) -> int:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def solution(length=5000) -> int:
def solution(length: int = 5000) -> int:

"""Ways to write sum of primes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Ways to write sum of primes
"""
Ways to write the sum of primes


Keyword arguments:
amount -- amount of different ways (default 5000)

Passing no parameters returns the result
>>> solution()
71
Comment on lines +26 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include more doctest

"""

primes = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
]

target = 11
while True:
ways = [1] + [0] * target
for p in primes:
for i in range(p, target + 1):
ways[i] += ways[i - p]
if ways[target] > length:
break
target += 1
return target


if __name__ == "__main__":
print(solution())