Skip to content

Commit 1914ea1

Browse files
authored
Merge pull request #112 from rubendelblanco/Problem4
Maths: ProjectEuler Problem 4
2 parents 8fb44ca + 75f9883 commit 1914ea1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* [Problem1](./Maths/ProjectEuler/Problem1.php)
4040
* [Problem2](./Maths/ProjectEuler/Problem2.php)
4141
* [Problem3](./Maths/ProjectEuler/Problem3.php)
42+
* [Problem4](./Maths/ProjectEuler/Problem4.php)
4243
* [Problem5](./Maths/ProjectEuler/Problem5.php)
4344
* [Problem6](./Maths/ProjectEuler/Problem6.php)
4445
* [Problem7](./Maths/ProjectEuler/Problem7.php)

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';
@@ -30,6 +31,11 @@ public function testProblem3(): void
3031
$this->assertSame(6857, problem3());
3132
}
3233

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

0 commit comments

Comments
 (0)