Skip to content

Commit d6f0e1e

Browse files
authored
Improve PowerSum algorithm implementation and documentation
This commit enhances the PowerSum class in the backtracking package. The changes focus on improving code quality, readability, and documentation. Key improvements include: 1. Enhanced code structure and efficiency: - Removed class-level variables for better thread safety - Optimized the recursive approach to avoid unnecessary calculations - Simplified the overall logic for easier understanding 2. Improved readability: - Used more descriptive variable names (e.g., 'targetSum' instead of 'n', 'power' instead of 'x') - Enhanced method structure with a private recursive helper method 3. Better documentation: - Added comprehensive JavaDoc comments explaining the algorithm's purpose and implementation - Clarified the meaning of parameters, especially relating them to the original problem statement (N and X) - Improved inline comments for better code understanding 4. Adhered to Java best practices: - Improved encapsulation by making the recursive method private - Used Math.pow() directly instead of a custom power method 5. Maintained core functionality: - The algorithm still solves the same problem as before, but with improved code quality
1 parent f721723 commit d6f0e1e

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed
Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
package com.thealgorithms.backtracking;
22

3-
/*
4-
* Problem Statement :
5-
* Find the number of ways that a given integer, N , can be expressed as the sum of the Xth powers
6-
* of unique, natural numbers. For example, if N=100 and X=3, we have to find all combinations of
7-
* unique cubes adding up to 100. The only solution is 1^3+2^3+3^3+4^3. Therefore output will be 1.
3+
/**
4+
* Problem Statement:
5+
* Find the number of ways that a given integer, N, can be expressed as the sum of the Xth powers
6+
* of unique, natural numbers.
7+
* For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100.
8+
* The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1.
9+
*
10+
* N is represented by the parameter 'targetSum' in the code.
11+
* X is represented by the parameter 'power' in the code.
812
*/
913
public class PowerSum {
1014

11-
private int count = 0;
12-
private int sum = 0;
13-
14-
public int powSum(int n, int x) {
15-
sum(n, x, 1);
16-
return count;
15+
/**
16+
* Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers.
17+
*
18+
* @param targetSum The target sum to achieve (N in the problem statement)
19+
* @param power The power to raise natural numbers to (X in the problem statement)
20+
* @return The number of ways to express the target sum
21+
*/
22+
public int powSum(int targetSum, int power) {
23+
return sumRecursive(targetSum, power, 1, 0);
1724
}
1825

19-
// here i is the natural number which will be raised by X and added in sum.
20-
public void sum(int n, int x, int i) {
21-
// if sum is equal to N that is one of our answer and count is increased.
22-
if (sum == n) {
23-
count++;
24-
return;
25-
} // we will be adding next natural number raised to X only if on adding it in sum the
26-
// result is less than N.
27-
else if (sum + power(i, x) <= n) {
28-
sum += power(i, x);
29-
sum(n, x, i + 1);
30-
// backtracking and removing the number added last since no possible combination is
31-
// there with it.
32-
sum -= power(i, x);
26+
/**
27+
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
28+
*
29+
* @param remainingSum The remaining sum to achieve
30+
* @param power The power to raise natural numbers to (X in the problem statement)
31+
* @param currentNumber The current natural number being considered
32+
* @param currentSum The current sum of powered numbers
33+
* @return The number of valid combinations
34+
*/
35+
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
36+
int newSum = currentSum + (int) Math.pow(currentNumber, power);
37+
38+
if (newSum == remainingSum) {
39+
return 1;
3340
}
34-
if (power(i, x) < n) {
35-
// calling the sum function with next natural number after backtracking if when it is
36-
// raised to X is still less than X.
37-
sum(n, x, i + 1);
41+
if (newSum > remainingSum) {
42+
return 0;
3843
}
39-
}
4044

41-
// creating a separate power function so that it can be used again and again when required.
42-
private int power(int a, int b) {
43-
return (int) Math.pow(a, b);
45+
return sumRecursive(remainingSum, power, currentNumber + 1, newSum) +
46+
sumRecursive(remainingSum, power, currentNumber + 1, currentSum);
4447
}
4548
}

0 commit comments

Comments
 (0)