Skip to content

Commit f3b2a94

Browse files
Improve power sum algorithm (#5652)
* Update directory * 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 * updated PowerSum * Refactor PowerSum algorithm implementation and documentation * Refactor PowerSum algorithm implementation and documentation * Refactor code formatting and remove unnecessary line in PowerSum.java * Refactor code formatting and add newline at end of file in .clang-format --------- Co-authored-by: manishraj27 <[email protected]> Co-authored-by: Bama Charan Chhandogi <[email protected]>
1 parent d437d58 commit f3b2a94

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
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+
// Special case: when both targetSum and power are zero
24+
if (targetSum == 0 && power == 0) {
25+
return 1; // by convention, one way to sum to zero: use nothing
26+
}
27+
return sumRecursive(targetSum, power, 1, 0);
1728
}
1829

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);
30+
/**
31+
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
32+
*
33+
* @param remainingSum The remaining sum to achieve
34+
* @param power The power to raise natural numbers to (X in the problem statement)
35+
* @param currentNumber The current natural number being considered
36+
* @param currentSum The current sum of powered numbers
37+
* @return The number of valid combinations
38+
*/
39+
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
40+
int newSum = currentSum + (int) Math.pow(currentNumber, power);
41+
42+
if (newSum == remainingSum) {
43+
return 1;
3344
}
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);
45+
if (newSum > remainingSum) {
46+
return 0;
3847
}
39-
}
4048

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);
49+
return sumRecursive(remainingSum, power, currentNumber + 1, newSum) + sumRecursive(remainingSum, power, currentNumber + 1, currentSum);
4450
}
4551
}

0 commit comments

Comments
 (0)