O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}