I've defined a function that computes the sum of the base-b digits of n:
DigitSum[n_, b_] := Total[IntegerDigits[n, b]]
Then I defined a function that computes the sum of the base-b digits of all of the integers up to x:
CumDigitSum[x_, b_] := Sum[DigitSum[n, b], {n, 1, x}]
Using these functions, I get
CumDigitSum[1000000,10]=27000001
which is correct. But then for larger inputs I get nonsense like
CumDigitSum[1000001,10]=500011500011
If I work in a different base, the same thing happens: at exactly 1000001, Mathematica begins computing the sum incorrectly. If I bypass my user-defined functions and just write what I mean, I get the correct answer:
Sum[Total[IntegerDigits[n, 10]], {n, 1, 1000001}] = 27000003
Any idea what could be happening here?
DigitSum[n_?NumericQ, b_] := Total[IntegerDigits[n, b]]it works (or at least it seems so) – Dr. belisarius Sep 08 '14 at 22:30Method -> "Procedural". – Daniel Lichtblau Sep 08 '14 at 22:40