Skip to content

Commit 40f4c51

Browse files
mindauglMaximSmolskiypre-commit-ci[bot]
authored
Add solution for the Euler problem 190 (#12664)
* Add solution for the Euler project problem 164. * Add solution for the Euler project problem 190. * Delete project_euler/problem_164/sol1.py * Delete project_euler/problem_164/__init__.py * Update sol1.py * Update sol1.py * Update sol1.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 145879b commit 40f4c51

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

project_euler/problem_190/__init__.py

Whitespace-only changes.

project_euler/problem_190/sol1.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Project Euler Problem 190: https://projecteuler.net/problem=190
3+
4+
Maximising a Weighted Product
5+
6+
Let S_m = (x_1, x_2, ..., x_m) be the m-tuple of positive real numbers with
7+
x_1 + x_2 + ... + x_m = m for which P_m = x_1 * x_2^2 * ... * x_m^m is maximised.
8+
9+
For example, it can be verified that |_ P_10 _| = 4112
10+
(|_ _| is the integer part function).
11+
12+
Find Sum_{m=2}^15 = |_ P_m _|.
13+
14+
Solution:
15+
- Fix x_1 = m - x_2 - ... - x_m.
16+
- Calculate partial derivatives of P_m wrt the x_2, ..., x_m. This gives that
17+
x_2 = 2 * x_1, x_3 = 3 * x_1, ..., x_m = m * x_1.
18+
- Calculate partial second order derivatives of P_m wrt the x_2, ..., x_m.
19+
By plugging in the values from the previous step, can verify that solution is maximum.
20+
"""
21+
22+
23+
def solution(n: int = 15) -> int:
24+
"""
25+
Calculate sum of |_ P_m _| for m from 2 to n.
26+
27+
>>> solution(2)
28+
1
29+
>>> solution(3)
30+
2
31+
>>> solution(4)
32+
4
33+
>>> solution(5)
34+
10
35+
"""
36+
total = 0
37+
for m in range(2, n + 1):
38+
x1 = 2 / (m + 1)
39+
p = 1.0
40+
for i in range(1, m + 1):
41+
xi = i * x1
42+
p *= xi**i
43+
total += int(p)
44+
return total
45+
46+
47+
if __name__ == "__main__":
48+
print(f"{solution() = }")

0 commit comments

Comments
 (0)