I'm not sure why this code is giving the error :(
f[0] = 0;
f[n_] := f[n] = DigitCount[n, 10, 1] + f[n - 1];
f[1016]

Just a suggestion. Try to use tail recursion to free the stack.
ClearAll[f,g];
g[n_] := g[n] = f[n, 0]
f[0, x_] := x;
f[n_, upto_] := f[n - 1, upto + DigitCount[n, 10, 1]]
g[2000]
(*1600*)
ClearAll[f]then try again. – Szabolcs Mar 02 '15 at 20:09f[1016]is 326. – bill s Mar 02 '15 at 20:411050, which is greater than1024, while in the code you posted there's1016. It is true thatf[1050]will cause Mathematica to exceed the recursion limit. You can increase$RecursionLimitto solve this.f[1016]should not trigger the error. – Szabolcs Mar 02 '15 at 20:57Block[{$RecursionLimit = 10^6}, f[5000]]for example. You need to understand what "recursion limit" means - the default recursion limit is 1024 and if you truly understood the limit you would realize that you have to increase it if you want evaluate the function for higher numbers. – C. E. Mar 02 '15 at 21:45