# How can I find the sum of the even digits of a number of all elements of a list?

Let $E(n)$ denote the sum of the even digits of $n$. For example, $E(123456789) = 2 + 4 + 6 + 8$. I tried

a = IntegerDigits[123456789]

and

Total[Select[a, EvenQ]]

Now, I want to find the sum $$S= E(1)+ E(2)+\cdots + E(2014).$$ I tried

tab = Table[i, {i, 2014}]

With the 2012-th, I tried

Total[Select[IntegerDigits[tab[[2012]]], EvenQ]]

But, I do not how to find the sum $S$. How can I do it with Mathematica?

-
Strangely coincidental timing with this. Is this part of an assignment? –  Mr.Wizard Dec 20 '13 at 10:38
From prior questions I know you are not unfamiliar with writing a function in Mathematica. Why not define Total[Select[IntegerDigits[number], EvenQ]] as a function f and then use Sum[f[n], {n, 1, 2014}]? –  Mr.Wizard Dec 20 '13 at 10:41
@ Mr.Wizard. Thank you for your help. –  minthao_2011 Dec 21 '13 at 1:45

tr = Total@Range[2, 8, 2] (*tr == 20*)

e[kkkk_, mmmm_] :=
mmmm (tr*kkkk*10^(kkkk - 1)) + 10^kkkk*Total@Range[2, mmmm - 1, 2] +
Function[If[EvenQ@#, #, 0]]@mmmm

bigE[num_] :=
Block[{digits = IntegerDigits[num], len}
,
len = Length@digits;
Total[e @@@ Transpose[{Range[0, len - 1], Reverse@digits}]] +
Total[Function[xx, If[EvenQ@xx, xx, 0]][First[#]]*
FromDigits[Rest@#] & /@ Table[digits[[k ;;]], {k, len}]]
]

bigE[2014]

12056

Don't stare directly at it :P

Timing comparison

n = 123234;
With[{temp = Flatten[IntegerDigits /@ Range[n]]},
Total@Pick[temp, Mod[temp, 2], 0]] // Timing
Total[f /@ Range[n]] // Timing
bigE[n]//Timing

{0.300975, 1187426}
{1.167729, 1187426}
{0.000194, 1187426}

also

bigE[123123347173459139491384] // Timing

{0.000700, 80816022876813366372150943062694366}

-
The e@@@Tranpose[Range[],...] thingy is very much like MapIndexed[e, ...] –  Jacob Akkerboom Dec 20 '13 at 12:27
Very instructive code for me. –  ubpdqn Dec 20 '13 at 14:10
@ubpdqn thanks, that's nice to hear :) –  Jacob Akkerboom Dec 20 '13 at 14:39
Jacob, this is the kind of optimization that comes in handy with Project Euler problems. Have you tried your hand at those? –  Mr.Wizard Dec 20 '13 at 15:13
@Mr.Wizard ah yes, I should try some of those soon :). I have done one or two problems years ago. I must say comments I saw by you as well as your profile have been a good reminder of the project. But this tipped me over the edge, I have just made an account again :). –  Jacob Akkerboom Dec 20 '13 at 15:44

maybe not what you had in mind, but not so slow either:

With[{temp = Flatten[IntegerDigits /@ Range[n]]},
Total@Pick[temp, Mod[temp, 2], 0]]
-
Good start (+1). This is somewhat shorter and faster: #.(1 - Mod[#, 2]) & @ Flatten @ IntegerDigits @ Range @ n –  Mr.Wizard Dec 20 '13 at 10:54
nice one - I tried to keep it readable up to some point, as I currently have no time to comment really :) but I like Dot there! –  Pinguin Dirk Dec 20 '13 at 10:55
Okay, how about: #.Boole[EvenQ@#] & @ Flatten @ IntegerDigits @ Range @ n ? Not as fast but perhaps more readable. –  Mr.Wizard Dec 20 '13 at 10:57
No, that's not worth it, as it's not much faster than the more direct: Tr @ Select[Flatten @ IntegerDigits @ Range @ n, EvenQ] –  Mr.Wizard Dec 20 '13 at 10:58
yes, here I have Dot>Mine>Boole>Trapproach in timings, for n=10^6 –  Pinguin Dirk Dec 20 '13 at 11:01
f[n_] := Total@Cases[IntegerDigits[n], _?EvenQ]
Total[f /@ Range[2014]]

yields:

12056

-