function sum = digit_sum(n)
if (~isscalar(n) || n < 0 || n ~= fix(n))
    error('input argument must be a positive integer');
end

if n == 0
    sum = 0;
else
    sum = rem(n,10) + digit_sum(fix(n/10));
end