patternjavaMinor
Custom Sum implementation
Viewed 0 times
customimplementationsum
Problem
There is a custom summary function needed as to receive an integer input (e.g.
In every iteration I intended to extract the right digit by
32456) and returns 3+2+4+5+6. Can you think of any other (or better) solution besides this?public class CustomSum {
public int sum(int input) {
int sum = 0;
while ( input >= 1 ) {
sum += input % 10;
input = input / 10;
}
return sum;
}
public static void main(String[] args) {
int input = Integer.parseInt(args[0]); //assuming args[0] is integer
CustomSum c = new CustomSum();
int sum = c.sum(input);
System.out.println("sum: " + sum);
}
}In every iteration I intended to extract the right digit by
input % 10, and omit the extracted right digit for next iteration by input / 10. Now consider when we are in last iteration and have a 1 digit number. The input / 10 would be greater than 0 and less than 1 (if actually it was float or double not int). So I didn't actually changed that specific condition, although with int it will always be 0.Solution
Your
Your current loop condition is
Further, your code excludes all negative values. What about the sum of
That makes me think that the code should all be looped on the condition
I was initially tempted to suggest that you have the code:
Unfortunately, that has a flaw with Integer.MIN_VALUE (the abs of Integer.MIN_VALUE is.... Integer.MIN_VALUE - a negative number). The net result, is that I considered doing an abs on each digit value inside the loop, but then realized, that all the digits will be negative, so the 'sum' will just be a large negative value, and we can take the absolute of the sum safely, because the sum of the digits will never be large enough to be a problem...
Thus, the solution I would recommend is:
That code (adjusted to fit in to my Eclipse IDE with a static method call, and different name), will work for any input value, of any sign, and ignore the sign in the result.
sum(...) function is fine, given the specification, but there is jut an issue of input validation... what about negative values?Your current loop condition is
input >= 1. This would be better written as input > 0. When you see the >= 1 it makes you wonder if there's a condition that's weird. In this case, there's not.Further, your code excludes all negative values. What about the sum of
-123?That makes me think that the code should all be looped on the condition
input != 0, but that would sum -123 as -6, and not 6.I was initially tempted to suggest that you have the code:
input = Math.abs(input);
int sum = 0;
while ( input != 0 ) {
sum += input % 10;
input = input / 10;
}
return sum;Unfortunately, that has a flaw with Integer.MIN_VALUE (the abs of Integer.MIN_VALUE is.... Integer.MIN_VALUE - a negative number). The net result, is that I considered doing an abs on each digit value inside the loop, but then realized, that all the digits will be negative, so the 'sum' will just be a large negative value, and we can take the absolute of the sum safely, because the sum of the digits will never be large enough to be a problem...
Thus, the solution I would recommend is:
public static int sumSumAbs(int input) {
int sum = 0;
while ( input != 0 ) {
sum += input % 10;
input = input / 10;
}
return Math.abs(sum);
}That code (adjusted to fit in to my Eclipse IDE with a static method call, and different name), will work for any input value, of any sign, and ignore the sign in the result.
Code Snippets
input = Math.abs(input);
int sum = 0;
while ( input != 0 ) {
sum += input % 10;
input = input / 10;
}
return sum;public static int sumSumAbs(int input) {
int sum = 0;
while ( input != 0 ) {
sum += input % 10;
input = input / 10;
}
return Math.abs(sum);
}Context
StackExchange Code Review Q#84083, answer score: 6
Revisions (0)
No revisions yet.