-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add power sum problem #8832
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
Add power sum problem #8832
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5e81a98
Add powersum problem
duongoku d6c02d3
Add doctest
duongoku f04de6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e4c08ec
Merge branch 'master' of https://github.com/TheAlgorithms/Python
duongoku 8c234ab
Merge branch 'master' of https://github.com/duongoku/Python
duongoku 0190498
Add more doctests
duongoku a9df3cc
Merge branch 'TheAlgorithms:master' into master
duongoku 12f5f37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6ac9414
Add more doctests
duongoku d4e18f1
Improve paramater name
duongoku 586a4cf
Fix line too long
duongoku ef288b0
Remove global variables
duongoku 990f115
Apply suggestions from code review
cclauss 70322f8
Apply suggestions from code review
cclauss 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
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,93 @@ | ||
""" | ||
Problem source: https://www.hackerrank.com/challenges/the-power-sum/problem | ||
Find the number of ways that a given integer X, can be expressed as the sum | ||
of the Nth powers of unique, natural numbers. For example, if X=13 and N=2. | ||
We have to find all combinations of unique squares adding up to 13. | ||
The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10. | ||
""" | ||
|
||
from math import pow | ||
|
||
|
||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def backtrack( | ||
needed_sum: int, | ||
power: int, | ||
current_number: int, | ||
current_sum: int, | ||
solutions_count: int, | ||
) -> tuple[int, int]: | ||
""" | ||
>>> backtrack(13, 2, 1, 0, 0) | ||
(0, 1) | ||
>>> backtrack(100, 2, 1, 0, 0) | ||
(0, 3) | ||
>>> backtrack(100, 3, 1, 0, 0) | ||
(0, 1) | ||
>>> backtrack(800, 2, 1, 0, 0) | ||
(0, 561) | ||
>>> backtrack(1000, 10, 1, 0, 0) | ||
(0, 0) | ||
>>> backtrack(400, 2, 1, 0, 0) | ||
(0, 55) | ||
>>> backtrack(50, 1, 1, 0, 0) | ||
(0, 3658) | ||
""" | ||
if current_sum == needed_sum: | ||
# If the sum of the powers is equal to needed_sum, then we have found a solution. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
solutions_count += 1 | ||
return current_sum, solutions_count | ||
|
||
i_to_n = int(pow(current_number, power)) | ||
if current_sum + i_to_n <= needed_sum: | ||
# If the sum of the powers is less than needed_sum, then we can continue adding powers. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
current_sum += i_to_n | ||
current_sum, solutions_count = backtrack( | ||
needed_sum, power, current_number + 1, current_sum, solutions_count | ||
) | ||
current_sum -= i_to_n | ||
if i_to_n < needed_sum: | ||
# If the power of i is less than needed_sum, then we can try with the next power. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
current_sum, solutions_count = backtrack( | ||
needed_sum, power, current_number + 1, current_sum, solutions_count | ||
) | ||
return current_sum, solutions_count | ||
|
||
|
||
def solve(needed_sum: int, power: int) -> int: | ||
""" | ||
>>> solve(13, 2) | ||
1 | ||
>>> solve(100, 2) | ||
3 | ||
>>> solve(100, 3) | ||
1 | ||
>>> solve(800, 2) | ||
561 | ||
>>> solve(1000, 10) | ||
0 | ||
>>> solve(400, 2) | ||
55 | ||
>>> solve(50, 1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid input | ||
needed_sum must be between 1 and 1000, power between 2 and 10. | ||
>>> solve(-10, 5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid input | ||
needed_sum must be between 1 and 1000, power between 2 and 10. | ||
""" | ||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not (1 <= needed_sum <= 1000 and 2 <= power <= 10): | ||
raise ValueError( | ||
"Invalid input\n" | ||
"needed_sum must be between 1 and 1000, power between 2 and 10." | ||
) | ||
|
||
return backtrack(needed_sum, power, 1, 0, 0)[1] # Return the solutions_count | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.