Skip to content

Solution to ProjectEuler 231 #11830

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
Closed
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
39 changes: 39 additions & 0 deletions project_euler/problem_231/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""

Check failure on line 1 in project_euler/problem_231/sol1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

project_euler/problem_231/sol1.py:1:1: INP001 File `project_euler/problem_231/sol1.py` is part of an implicit namespace package. Add an `__init__.py`.
Project Euler Problem 231: https://projecteuler.net/problem=231

The binomial coefficient choose(10, 3)=120.
120 = 2^3 * 3 * 5 = 2 * 2 * 2 * 3 * 5, and 2 + 2 + 2 + 3 + 5 = 14.
So the sum of the terms in the prime factorisation of choose(10, 3) is 14.

Find the sum of the terms in the prime factorisation of choose(20_000_000, 15_000_000).
"""

from sympy import factorint
from collections import Counter


def solution(n: int = 20_000_000, k: int = 15_000_000) -> int:

Check failure on line 15 in project_euler/problem_231/sol1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

project_euler/problem_231/sol1.py:11:1: I001 Import block is un-sorted or un-formatted

Check failure on line 15 in project_euler/problem_231/sol1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ARG001)

project_euler/problem_231/sol1.py:15:14: ARG001 Unused function argument: `n`
"""
Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(10, 3)
14
"""

a, b = 20_000_000, 15_000_000
top, bot = [], []
for t in range(b + 1, a + 1):
top.extend(factorint(t, multiple=True))
for b in range(1, a - b + 1):

Check failure on line 27 in project_euler/problem_231/sol1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B020)

project_euler/problem_231/sol1.py:27:9: B020 Loop control variable `b` overrides iterable it iterates
bot.extend(factorint(b, multiple=True))

top = Counter(top)
bot = Counter(bot)
s = 0
for k, v in top.items():

Check failure on line 33 in project_euler/problem_231/sol1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1704)

project_euler/problem_231/sol1.py:33:9: PLR1704 Redefining argument with the local name `k`
s += k * (v - bot[k])
return s


if __name__ == "__main__":
print(f"{solution() = }")
Loading