|
| 1 | +""" |
| 2 | +Problem source: https://www.hackerrank.com/challenges/the-power-sum/problem |
| 3 | +Find the number of ways that a given integer X, can be expressed as the sum |
| 4 | +of the Nth powers of unique, natural numbers. For example, if X=13 and N=2. |
| 5 | +We have to find all combinations of unique squares adding up to 13. |
| 6 | +The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10. |
| 7 | +""" |
| 8 | + |
| 9 | +from math import pow |
| 10 | + |
| 11 | + |
| 12 | +def backtrack( |
| 13 | + needed_sum: int, |
| 14 | + power: int, |
| 15 | + current_number: int, |
| 16 | + current_sum: int, |
| 17 | + solutions_count: int, |
| 18 | +) -> tuple[int, int]: |
| 19 | + """ |
| 20 | + >>> backtrack(13, 2, 1, 0, 0) |
| 21 | + (0, 1) |
| 22 | + >>> backtrack(100, 2, 1, 0, 0) |
| 23 | + (0, 3) |
| 24 | + >>> backtrack(100, 3, 1, 0, 0) |
| 25 | + (0, 1) |
| 26 | + >>> backtrack(800, 2, 1, 0, 0) |
| 27 | + (0, 561) |
| 28 | + >>> backtrack(1000, 10, 1, 0, 0) |
| 29 | + (0, 0) |
| 30 | + >>> backtrack(400, 2, 1, 0, 0) |
| 31 | + (0, 55) |
| 32 | + >>> backtrack(50, 1, 1, 0, 0) |
| 33 | + (0, 3658) |
| 34 | + """ |
| 35 | + if current_sum == needed_sum: |
| 36 | + # If the sum of the powers is equal to needed_sum, then we have a solution. |
| 37 | + solutions_count += 1 |
| 38 | + return current_sum, solutions_count |
| 39 | + |
| 40 | + i_to_n = int(pow(current_number, power)) |
| 41 | + if current_sum + i_to_n <= needed_sum: |
| 42 | + # If the sum of the powers is less than needed_sum, then continue adding powers. |
| 43 | + current_sum += i_to_n |
| 44 | + current_sum, solutions_count = backtrack( |
| 45 | + needed_sum, power, current_number + 1, current_sum, solutions_count |
| 46 | + ) |
| 47 | + current_sum -= i_to_n |
| 48 | + if i_to_n < needed_sum: |
| 49 | + # If the power of i is less than needed_sum, then try with the next power. |
| 50 | + current_sum, solutions_count = backtrack( |
| 51 | + needed_sum, power, current_number + 1, current_sum, solutions_count |
| 52 | + ) |
| 53 | + return current_sum, solutions_count |
| 54 | + |
| 55 | + |
| 56 | +def solve(needed_sum: int, power: int) -> int: |
| 57 | + """ |
| 58 | + >>> solve(13, 2) |
| 59 | + 1 |
| 60 | + >>> solve(100, 2) |
| 61 | + 3 |
| 62 | + >>> solve(100, 3) |
| 63 | + 1 |
| 64 | + >>> solve(800, 2) |
| 65 | + 561 |
| 66 | + >>> solve(1000, 10) |
| 67 | + 0 |
| 68 | + >>> solve(400, 2) |
| 69 | + 55 |
| 70 | + >>> solve(50, 1) |
| 71 | + Traceback (most recent call last): |
| 72 | + ... |
| 73 | + ValueError: Invalid input |
| 74 | + needed_sum must be between 1 and 1000, power between 2 and 10. |
| 75 | + >>> solve(-10, 5) |
| 76 | + Traceback (most recent call last): |
| 77 | + ... |
| 78 | + ValueError: Invalid input |
| 79 | + needed_sum must be between 1 and 1000, power between 2 and 10. |
| 80 | + """ |
| 81 | + if not (1 <= needed_sum <= 1000 and 2 <= power <= 10): |
| 82 | + raise ValueError( |
| 83 | + "Invalid input\n" |
| 84 | + "needed_sum must be between 1 and 1000, power between 2 and 10." |
| 85 | + ) |
| 86 | + |
| 87 | + return backtrack(needed_sum, power, 1, 0, 0)[1] # Return the solutions_count |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + import doctest |
| 92 | + |
| 93 | + doctest.testmod() |
0 commit comments