Given a positive integer ( n ), return its punishment number.
The punishment number of ( n ) is the sum of squares of all numbers ( i ) (where ( )) that satisfy:
Input:
n = 12
Output:
182
Explanation: Numbers satisfying the condition:
Total punishment number: ( 1 + 81 + 100 = 182 )
Input:
n = 25
Output:
182
Explanation: Numbers satisfying the condition:
Total punishment number: ( 1 + 81 + 100 = 182 )
https://leetcode.com/problems/find-the-punishment-number-of-an-integer/description/
Loading component...
Loading component...
Main Function is not defined.
INPUT: n = 25
OUTPUT: 182
public static int punishmentNumber(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int square = i * i;
boolean condition_varibale = canPartition(String.valueOf(square), i, 0, 0);
if(condition_variable){
sum += square;
}//If End
}//Loop End
return sum;
} //function end
private static boolean canPartition(String s, int target, int index, int currentSum) {
if (index == s.length()) {
return currentSum == target;
}//If End
int num = 0;
for (int i = index; i < s.length(); i++) {
num = num * 10 + (s.charAt(i) - '0');
int condition_variable = canPartition(s, target, i + 1, currentSum + num);
if(condtion_variable){
return true;
}//If End
}//Loop End
return false;
} //function end