-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem-4.php
52 lines (42 loc) · 1.23 KB
/
problem-4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
require_once 'functions/palindrome.php';
/**
* Problem 4 :
* ===========
*
* 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.
*
* @see https://projecteuler.net/problem=4
*
* @param int $nbDigits
*
* @return int
*/
function problem4(int $nbDigits = 3): int
{
// Fill an array with numbers from 10... to 99...
$startIndex = (int)str_pad('1', $nbDigits, '0');
$num = (int)str_pad('9', $nbDigits, '0');
$numbers = array_reverse(array_keys(array_fill($startIndex, $num, 0)));
$max = 0;
// Keep the best N for early break. No need of the best M, because we iterate from top to bottom.
$bestN = 0;
foreach ($numbers as $m) {
foreach ($numbers as $n) {
// Break early if $n less than our best match.
if ($n <= $bestN) {
break;
}
$product = $m * $n;
if ($product > $max && isPalindrome((string)$product)) {
$bestN = $n;
$max = $product;
}
}
}
return $max;
}