Add problem 4 from Project Euler

This commit is contained in:
Ruben del Blanco 2023-04-13 11:40:19 +02:00
parent 79f5d2235b
commit 8b3a173e5d
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,28 @@
<?php
/**
* Problem:
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
* Find the largest palindrome made from the product of two 3-digit numbers.
*/
/**
* @return int
*/
function problem4(): int
{
$largest = 0;
for ($i=100; $i<1000; $i++){
for ($j=$i; $j<1000; $j++) {
$product = $i*$j;
if ( strrev((string)$product) == (string)$product && $product > $largest) {
$largest = $product;
}
}
}
return $largest;
}

View File

@ -6,6 +6,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem1.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem2.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem3.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem4.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem5.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem6.php';
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem7.php';
@ -29,6 +30,11 @@ class ProjectEulerTest extends TestCase
$this->assertSame(6857, problem3());
}
public function testProblem4(): void
{
$this->assertSame(906609, problem4());
}
public function testProblem5(): void
{
$this->assertSame(232792560, problem5());