Project Euler Problem 4

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

PHP Script

for($a = 100; $a < 1000; $a++) {
	for($b = $a; $b < 1000; $b++) {
		$sum = $a * $b;
		if(strrev($sum) == $sum) {
			$arr[] = $sum;
echo $arr[count($arr) - 1];

You can check whether a string is a palindrome by using the strrev function, which reverses the string. For those who are curious, I use $b = $a instead of $b = 100 since we only need to multiply $a by the numbers bigger than itself. This is so we don’t have to multiply 100 * 500 and 500 * 100. Using $b = $a instead of $b = 100 reduces the time required by half. Although it’s not any noticeable difference, it’s still faster.

In my opinion, this is probably one of the easiest “challenges,” since all we have to do is multiply every possible 3 digit number by each other and check if they are a palindrome.

Leave a Reply

Your email address will not be published.