Duncan's blog

September 26, 2014

Project Euler: problem 8 (PHP) – Largest product in a series

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

88888888I 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 8:


The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

This question has been slightly changed since I originally did this. Previously it was the product of 5 adjacent digits; now it’s the product of 13 adjacent digits.

<?php
$digits = <<<FOO
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
FOO;

$max = 0;
$limit = 13;

// turn this into a single-line string
$digits = str_replace(["\n", "\r"], "", $digits);

for ($i = 0; $i < strlen($digits); $i++) {
	$sum = 1;
	
	// multiply together our 13 digits
	for ($j = 0; $j < $limit; $j++) {
		$sum *= substr($digits, $i + $j, 1);
	}
	
	if ($sum > $max) {
		$max = $sum;
	}
}

echo $max;

So I’m using the Heredoc syntax, equivalent to cfsavecontent, for a multi-line string. I’m not sure if it would be advantageous in this case to use Nowdoc, or even just appending each line to a single string, but for now Heredoc seems simple and does what I’m looking for.

I expect there’s some de facto convention for what to use for the end identifier (the bit after the <<< and again at the end of the string), but for now FOO will suffice.   Is it covered in any of the PSR coding standards?

So I get rid of the newline and carriage return characters from the string, turning it into one single line string.  Initially I tried doing str_replace([‘\n’, ‘\r’], “”, $digits) except that didn’t work because it didn’t like the single quoted characters.  Instead I had to double-quote them, obviously!

Again we find inconsistencies in PHP, this time with the naming of the functions, str_replace() but strlen(), being the equivalent of ColdFusion’s replace() and len().

And substr(string, start[, count]) being more fully-featured than CFML’s mid(string, start, count).  If you use negative numbers it can extract starting from the end of the string.  And if you don’t specify the count parameter, it goes up to the end of the string.

Leave a Comment »

No comments yet.

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

Blog at WordPress.com.

%d bloggers like this: