-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add solution for the Euler problem 190 #12664
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 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
55a9a41
Add solution for the Euler project problem 164.
mindaugl 2b883d1
Add solution for the Euler project problem 190.
mindaugl cda3c1d
Delete project_euler/problem_164/sol1.py
MaximSmolskiy d93526f
Delete project_euler/problem_164/__init__.py
MaximSmolskiy c5c5752
Update sol1.py
MaximSmolskiy 2ee900c
Update sol1.py
MaximSmolskiy df249f2
Update sol1.py
MaximSmolskiy 7462729
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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,48 @@ | ||
""" | ||
Project Euler Problem 190: https://projecteuler.net/problem=190 | ||
|
||
Maximising a Weighted Product | ||
|
||
Let S_m = (x_1, x_2, ..., x_m) be the m-tuple of positive real numbers with | ||
x_1 + x_2 + ... + x_m = m for which P_m = x_1 * x_2^2 * ... * x_m^m is maximised. | ||
|
||
For example, it can be verified that |_ P_10 _| = 4112 | ||
(|_ _| is the integer part function). | ||
|
||
Find Sum_{m=2}^15 = |_ P_m _|. | ||
|
||
Solution: | ||
- Fix x_1 = m - x_2 - ... - x_m. | ||
- Calculate partial derivatives of P_m wrt the x_2, ..., x_m. This gives that | ||
x_2 = 2 * x_1, x_3 = 3 * x_1, ..., x_m = m * x_1. | ||
- Calculate partial second order derivatives of P_m wrt the x_2, ..., x_m. | ||
By plugging in the values from the previous step, can verify that solution is maximum. | ||
""" | ||
|
||
|
||
def solution(n: int = 15) -> int: | ||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Calculate sum of |_ P_m _| for m from 2 to n. | ||
|
||
>>> solution(2) | ||
1 | ||
>>> solution(3) | ||
2 | ||
>>> solution(4) | ||
4 | ||
>>> solution(5) | ||
10 | ||
""" | ||
total = 0 | ||
for m in range(2, n + 1): | ||
x1 = 2 / (m + 1) | ||
p = 1.0 | ||
for i in range(1, m + 1): | ||
xi = i * x1 | ||
p *= xi ** i | ||
total += int(p) | ||
return total | ||
|
||
|
||
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.