Skip to content

Commit 8b3a173

Browse files
Add problem 4 from Project Euler
1 parent 79f5d22 commit 8b3a173

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Maths/ProjectEuler/Problem4.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Problem:
5+
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
6+
* Find the largest palindrome made from the product of two 3-digit numbers.
7+
*/
8+
9+
/**
10+
* @return int
11+
*/
12+
function problem4(): int
13+
{
14+
$largest = 0;
15+
16+
for ($i=100; $i<1000; $i++){
17+
18+
for ($j=$i; $j<1000; $j++) {
19+
$product = $i*$j;
20+
21+
if ( strrev((string)$product) == (string)$product && $product > $largest) {
22+
$largest = $product;
23+
}
24+
}
25+
}
26+
27+
return $largest;
28+
}

tests/Maths/ProjectEulerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem1.php';
77
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem2.php';
88
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem3.php';
9+
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem4.php';
910
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem5.php';
1011
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem6.php';
1112
require_once __DIR__ . '/../../Maths/ProjectEuler/Problem7.php';
@@ -29,6 +30,11 @@ public function testProblem3(): void
2930
$this->assertSame(6857, problem3());
3031
}
3132

33+
public function testProblem4(): void
34+
{
35+
$this->assertSame(906609, problem4());
36+
}
37+
3238
public function testProblem5(): void
3339
{
3440
$this->assertSame(232792560, problem5());

0 commit comments

Comments
 (0)