Duncan's blog

September 22, 2014

Project Euler: problem 4 (PHP) – Largest palindrome product

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

Stockwell graffitiI 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 4:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Nice and easy:

<?php
$palindrome = 0;

for ($i = 100; $i <= 999; $i++) {
	for ($j = 100; $j <= 999; $j++) {
		$x = $i * $j;
		if ($x > $palindrome && $x == strrev($x)) {
			$palindrome = $x;
		}
	}
}

echo $palindrome;


strrev() being the PHP equivalent of ColdFusion’s more obviously-named reverse().

Advertisements

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

Create a free website or blog at WordPress.com.

%d bloggers like this: