Duncan's blog

September 30, 2014

Project Euler: problem 13 (PHP) – Large sum

Filed under: PHP,Project Euler — duncan @ 8:00 am

13I 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 I’d appreciate any feedback on my PHP code.

Problem 13:

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

… etc.


$numbers = <<<FOO

$sum = 0;

$lines = explode("\r\n", $numbers);

foreach($lines as $line) {
	$sum += $line;

echo $sum;

So taking exactly the same approach as with ColdFusion.  And I get the same problem of having the number being output in scientific notation.  But again the first ten digits I need are visible without needing to do anything clever to output the entire number.  Which is a good job, as I’m not sure what I’d need to do!



  1. All find but the last line should be:

    echo substr($sum, 0, 10);

    Comment by sciencefictionbookclub — September 30, 2014 @ 7:01 pm | Reply

    • ah cool, yes of course

      Comment by duncan — September 30, 2014 @ 9:53 pm | 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: