-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Add power sum problem #8832
Changes from 5 commits
5e81a98
d6c02d3
f04de6a
e4c08ec
8c234ab
0190498
a9df3cc
12f5f37
6ac9414
d4e18f1
586a4cf
ef288b0
990f115
70322f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
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. | ||
""" | ||
|
||
from math import pow | ||
|
||
sum_ = 0 | ||
count_ = 0 | ||
|
||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def backtrack(x: int, n: int, i: int) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: Please provide descriptive name for the parameter:
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Backtracking function to find all the possible combinations | ||
of powers of natural numbers that add up to x. | ||
Parameters: | ||
x: The number to be expressed as the sum of | ||
the nth powers of unique, natural numbers. | ||
n: The power of the natural numbers. | ||
i: The current natural number. | ||
|
||
Returns: | ||
None | ||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> backtrack(13, 2, 1) | ||
|
||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
global sum_, count_ | ||
i_to_n = int(pow(i, n)) | ||
if sum_ == x: | ||
# If the sum of the powers is equal to x, then we have found a solution. | ||
global count_ | ||
count_ += 1 | ||
return | ||
elif sum_ + i_to_n <= x: | ||
# If the sum of the powers is less than x, then we can continue adding powers. | ||
sum_ += i_to_n | ||
backtrack(x, n, i + 1) | ||
sum_ -= i_to_n | ||
if i_to_n < x: | ||
# If the power of i is less than x, then we can try with the next power. | ||
backtrack(x, n, i + 1) | ||
return | ||
|
||
|
||
def solve(x: int, n: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
Calculates the number of ways that x can be expressed as the sum of | ||
the nth powers of unique, natural numbers. | ||
Parameters: | ||
x: The number to be expressed as the sum of | ||
the nth powers of unique, natural numbers. | ||
n: The power of the natural numbers. | ||
|
||
Returns: | ||
The number of ways that x can be expressed as the sum of | ||
the nth powers of unique, natural numbers. | ||
|
||
>>> solve(13, 2) | ||
1 | ||
""" | ||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
global sum_, count_ | ||
sum_, count_ = 0, 0 | ||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
backtrack(x, n, 1) | ||
return count_ | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
# x = 100 | ||
# n = 3 | ||
# output = f"The number of ways that {x} can be expressed as the sum of" | ||
# output += f" the {n}th powers of unique, natural numbers is {solve(x, n)}" | ||
# print(output) | ||
# Output: The number of ways that 100 can be expressed as the | ||
# sum of the 3th powers of unique, natural numbers is 1 | ||
duongoku marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.