-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Solution for the Euler Project Problem 122 #12655
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f86430b
Add initial version for euler project problem 122.
mindaugl 9a38038
Add doctests and documentation for the project euler problem 122.
mindaugl 53297f7
Update sol1.py
MaximSmolskiy 3ec91f3
Update sol1.py
MaximSmolskiy f03cdc3
Update sol1.py
MaximSmolskiy 32811cd
Update sol1.py
MaximSmolskiy 30b351b
Update sol1.py
MaximSmolskiy b64e230
Update sol1.py
MaximSmolskiy 136f622
Update sol1.py
MaximSmolskiy 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,89 @@ | ||
""" | ||
Project Euler Problem 122: https://projecteuler.net/problem=122 | ||
|
||
Efficient Exponentiation | ||
|
||
The most naive way of computing n^15 requires fourteen multiplications: | ||
|
||
n x n x ... x n = n^15. | ||
|
||
But using a "binary" method you can compute it in six multiplications: | ||
|
||
n x n = n^2 | ||
n^2 x n^2 = n^4 | ||
n^4 x n^4 = n^8 | ||
n^8 x n^4 = n^12 | ||
n^12 x n^2 = n^14 | ||
n^14 x n = n^15 | ||
|
||
However it is yet possible to compute it in only five multiplications: | ||
|
||
n x n = n^2 | ||
n^2 x n = n^3 | ||
n^3 x n^3 = n^6 | ||
n^6 x n^6 = n^12 | ||
n^12 x n^3 = n^15 | ||
|
||
We shall define m(k) to be the minimum number of multiplications to compute n^k; | ||
for example m(15) = 5. | ||
|
||
Find sum_{k = 1}^200 m(k). | ||
|
||
It uses the fact that for rather small n, applicable for this problem, the solution | ||
for each number can be formed by increasing the largest element. | ||
|
||
References: | ||
- https://en.wikipedia.org/wiki/Addition_chain | ||
""" | ||
|
||
|
||
def solve(nums: list[int], goal: int, depth: int) -> bool: | ||
""" | ||
Checks if nums can have a sum equal to goal, given that length of nums does | ||
not exceed depth. | ||
|
||
>>> solve([1], 2, 2) | ||
True | ||
>>> solve([1], 2, 0) | ||
False | ||
""" | ||
if len(nums) > depth: | ||
return False | ||
for el in nums: | ||
if el + nums[-1] == goal: | ||
return True | ||
nums.append(el + nums[-1]) | ||
if solve(nums=nums, goal=goal, depth=depth): | ||
return True | ||
del nums[-1] | ||
return False | ||
|
||
|
||
def solution(n: int = 200) -> 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
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Calculates sum of smallest number of multiplactions for each number up to | ||
and including n. | ||
|
||
>>> solution(1) | ||
0 | ||
>>> solution(2) | ||
1 | ||
>>> solution(14) | ||
45 | ||
>>> solution(15) | ||
50 | ||
""" | ||
total = 0 | ||
for i in range(2, n + 1): | ||
max_length = 0 | ||
while True: | ||
nums = [1] | ||
max_length += 1 | ||
if solve(nums=nums, goal=i, depth=max_length): | ||
break | ||
total += max_length | ||
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.