mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
* 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 <manishraj27@users.noreply.github.com> Co-authored-by: Bama Charan Chhandogi <b.c.chhandogi@gmail.com>
52 lines
2.1 KiB
Java
52 lines
2.1 KiB
Java
package com.thealgorithms.backtracking;
|
|
|
|
/**
|
|
* Problem Statement:
|
|
* Find the number of ways that a given integer, N, can be expressed as the sum of the Xth powers
|
|
* of unique, natural numbers.
|
|
* For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100.
|
|
* The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1.
|
|
*
|
|
* N is represented by the parameter 'targetSum' in the code.
|
|
* X is represented by the parameter 'power' in the code.
|
|
*/
|
|
public class PowerSum {
|
|
|
|
/**
|
|
* Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers.
|
|
*
|
|
* @param targetSum The target sum to achieve (N in the problem statement)
|
|
* @param power The power to raise natural numbers to (X in the problem statement)
|
|
* @return The number of ways to express the target sum
|
|
*/
|
|
public int powSum(int targetSum, int power) {
|
|
// Special case: when both targetSum and power are zero
|
|
if (targetSum == 0 && power == 0) {
|
|
return 1; // by convention, one way to sum to zero: use nothing
|
|
}
|
|
return sumRecursive(targetSum, power, 1, 0);
|
|
}
|
|
|
|
/**
|
|
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
|
|
*
|
|
* @param remainingSum The remaining sum to achieve
|
|
* @param power The power to raise natural numbers to (X in the problem statement)
|
|
* @param currentNumber The current natural number being considered
|
|
* @param currentSum The current sum of powered numbers
|
|
* @return The number of valid combinations
|
|
*/
|
|
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
|
|
int newSum = currentSum + (int) Math.pow(currentNumber, power);
|
|
|
|
if (newSum == remainingSum) {
|
|
return 1;
|
|
}
|
|
if (newSum > remainingSum) {
|
|
return 0;
|
|
}
|
|
|
|
return sumRecursive(remainingSum, power, currentNumber + 1, newSum) + sumRecursive(remainingSum, power, currentNumber + 1, currentSum);
|
|
}
|
|
}
|