3

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
minthao_2011
  • 4,503
  • 3
  • 31
  • 46

3 Answers3

6
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}

Jacob Akkerboom
  • 12,215
  • 45
  • 79
  • 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
  • @Jacob Good luck! I think you'll enjoy it. We can link accounts to see which problems each other has solved (to know what is fair to talk about etc.) if you are interested. The problems trend towards getting harder as you go up and I got stumped a few years ago around the time I found Stack Overflow. I keep meaning to give it another go but something always draws my interest away. – Mr.Wizard Dec 20 '13 at 16:41
3

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]]
Pinguin Dirk
  • 6,519
  • 1
  • 26
  • 36
  • 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
1
f[n_] := Total@Cases[IntegerDigits[n], _?EvenQ]
Total[f /@ Range[2014]]

yields:

12056

ubpdqn
  • 60,617
  • 3
  • 59
  • 148