3
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ Strangely coincidental timing with this. Is this part of an assignment? $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 10:38
  • $\begingroup$ 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}]? $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 10:41
  • $\begingroup$ @ Mr.Wizard. Thank you for your help. $\endgroup$ Dec 21, 2013 at 1:45

3 Answers 3

6
$\begingroup$
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}

$\endgroup$
6
  • $\begingroup$ The e@@@Tranpose[Range[],...] thingy is very much like MapIndexed[e, ...] $\endgroup$ Dec 20, 2013 at 12:27
  • $\begingroup$ Very instructive code for me. $\endgroup$
    – ubpdqn
    Dec 20, 2013 at 14:10
  • $\begingroup$ @ubpdqn thanks, that's nice to hear :) $\endgroup$ Dec 20, 2013 at 14:39
  • $\begingroup$ Jacob, this is the kind of optimization that comes in handy with Project Euler problems. Have you tried your hand at those? $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 15:13
  • $\begingroup$ @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 :). $\endgroup$ Dec 20, 2013 at 15:44
3
$\begingroup$

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]]
$\endgroup$
5
  • $\begingroup$ Good start (+1). This is somewhat shorter and faster: #.(1 - Mod[#, 2]) & @ Flatten @ IntegerDigits @ Range @ n $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 10:54
  • $\begingroup$ 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! $\endgroup$ Dec 20, 2013 at 10:55
  • $\begingroup$ Okay, how about: #.Boole[EvenQ@#] & @ Flatten @ IntegerDigits @ Range @ n ? Not as fast but perhaps more readable. $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 10:57
  • $\begingroup$ No, that's not worth it, as it's not much faster than the more direct: Tr @ Select[Flatten @ IntegerDigits @ Range @ n, EvenQ] $\endgroup$
    – Mr.Wizard
    Dec 20, 2013 at 10:58
  • $\begingroup$ yes, here I have Dot>Mine>Boole>Trapproach in timings, for n=10^6 $\endgroup$ Dec 20, 2013 at 11:01
1
$\begingroup$
f[n_] := Total@Cases[IntegerDigits[n], _?EvenQ]
Total[f /@ Range[2014]]

yields:

12056

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.