mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-03-17 13:09:43 +01:00
Add problem 4 from Project Euler
This commit is contained in:
parent
79f5d2235b
commit
8b3a173e5d
28
Maths/ProjectEuler/Problem4.php
Normal file
28
Maths/ProjectEuler/Problem4.php
Normal 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;
|
||||
}
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user