Problem 13:

*Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.*

*37107287533902102798797998220837590246510135740250*

46376937677490009712648124896970078050417018260538

74324986199524741059474233309513058123726617309629

… etc.

53503534226472524250874054075591789781264330331690

So this seems reasonably straightforward. Reusing our <cfsavecontent> technique from problem 8 and problem 11, we can just loop through the numbers, adding them up, right?

<cfsavecontent variable="number">
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
...
53503534226472524250874054075591789781264330331690
</cfsavecontent>
<cfset sum = 0>
<cfloop index="i" list="#number#" delimiters="#Chr(13)##Chr(10)#">
<cfset sum = sum + i>
</cfloop>
<cfoutput>#sum#</cfoutput>

This code returns a number in scientific notation, because of the limitations of 32-bit integers. If you’re using CFMX (I’m doing this in CF 5), you could just cast to a Java long int to get your answer. I tried a variety of ways to present the total sum, then realised the answer was right there on the screen.

The answer is in the form

1.23456789012E+051

i.e. the equivalent of

1.23456789012 x 10^{51}

i.e. the equivalent of

123456789012 followed by forty digits.

I’m only looking for the first ten digits, so I just manually removed the decimal and grabbed those digits. KISS!

### Like this:

Like Loading...

*Related*

[…] previously blogged about this Project Euler puzzle 6 years ago, using ColdFusion. This is my approach using PHP as a simple practical exercise for myself, and […]

Pingback by Project Euler: problem 13 (PHP) | Duncan's blog — September 30, 2014 @ 8:01 am |