Sum digits of an integer
This task takes a Natural Number in a given Base and returns the sum of it digits:
- 1 sums to 1;
- 1234 sums to 10;
- 0xfe sums to 29;
- 0xf0e sums to 29.
C++
<lang cpp>#include <iostream>
- include <cmath>
int SumDigits(const int digits, const int BASE = 10) {
int sum = 0; int x = digits; for (int i = log(digits)/log(BASE); i>0; i--) { const int z = pow(BASE, i); const int t = x / z; sum += t; x -= t * z; } return x + sum;
}
int main() {
std::cout << SumDigits(1) << ' ' << SumDigits(12345) << ' ' << SumDigits(123045) << ' ' << SumDigits(0xfe, 16) << ' ' << SumDigits(0xf0e, 16) << std::endl; return 0;
}</lang>
- Output:
1 15 15 29 29
Erlang
<lang erlang> -module(sum_digits). -export([sum_digits/2, sum_digits/1]).
sum_digits(N) ->
sum_digits(N,10).
sum_digits(N,B) ->
sum_digits(N,B,0).
sum_digits(0,_,Acc) ->
Acc;
sum_digits(N,B,Acc) when N < B ->
Acc+N;
sum_digits(N,B,Acc) ->
sum_digits(N div B, B, Acc + (N rem B)).
</lang>
Example usage:
2> sum_digits:sum_digits(1). 1 3> sum_digits:sum_digits(1234). 10 4> sum_digits:sum_digits(16#fe,16). 29 5> sum_digits:sum_digits(16#f0e,16). 29
J
<lang j>digsum=: 10&$: : (+/@(#.inv))</lang>
Example use:
<lang J> digsum 1234 10
10 digsum 254
11
16 digsum 254
29</lang>
Illustration of mechanics:
<lang j> 10 #. 1 2 3 4 1234
10 #.inv 1234
1 2 3 4
10 +/ 1 2 3 4
10
10 +/@(#.inv) 1234
10</lang>
So #.inv gives us the digits, +/ gives us the sum, and @ glues them together with +/ being a "post processor" for (#.inv). We need the parenthesis or inv will try to look up the inverse of +/@#. and that's not well defined.
The rest of it is about using 10 as the default left argument when no left argument is defined. A J verb has a monadic definition (for use with one argument) and a dyadic definition (for use with two arguments) and : derives a new verb where the monadic definition is used from the verb on the left and the dyadic definition is used from the verb on the right. $: is a self reference to the top-level defined verb.
Full examples:
<lang j> digsum 1 1
digsum 1234
10
16 digsum 16bfe
29
16 digsum 16bf0e
29</lang>
Note that J implements numeric types -- J tries to ensure that the semantics of numbers match their mathematical properties. So it doesn't matter how we originally obtained a number.
<lang j> 200+54 254
254
254
2.54e2
254
16bfe
254</lang>
Java
<lang java>public class SumDigits {
public static int sumDigits(long num) {
return sumDigits(num, 10);
} public static int sumDigits(long num, int base) {
String s = Long.toString(num, base); int result = 0; for (int i = 0; i < s.length(); i++) result += Character.digit(s.charAt(i), base); return result;
}
public static void main(String[] args) {
System.out.println(sumDigits(1)); System.out.println(sumDigits(12345)); System.out.println(sumDigits(123045)); System.out.println(sumDigits(0xfe, 16)); System.out.println(sumDigits(0xf0e, 16));
}
}</lang>
- Output:
1 15 15 29 29
Ruby
<lang ruby>>> def sumDigits(num, base = 10) >> num.to_s(base).split(//).inject(0) {|z, x| z + x.to_i(base)} >> end => nil >> sumDigits(1) => 1 >> sumDigits(12345) => 15 >> sumDigits(123045) => 15 >> sumDigits(0xfe, 16) => 29 >> sumDigits(0xf0e, 16) => 29 </lang>