Duncan's blog

October 30, 2008

Project Euler: problem 13

Filed under: Coldfusion,Project Euler — duncan @ 7:00 am
Tags: , , , , ,

Problem 13:

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

… etc.

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">

<cfset sum = 0>

<cfloop index="i" list="#number#" delimiters="#Chr(13)##Chr(10)#">
	<cfset sum = sum + i>


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
i.e. the equivalent of
1.23456789012 x 1051
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!


1 Comment »

  1. […] 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 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: