-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path52.php
46 lines (44 loc) · 1.01 KB
/
52.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
<!--
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
-->
<?php $startTime = microtime(true);
function isPermutation($a,$b) {
$numsCountA = array();
for ($i=0; $i <= 9; $i++) {
$numsCountA[$i] = substr_count($a,("".$i));
}
$numsCountB = array();
for ($i=0; $i <= 9; $i++) {
$numsCountB[$i] = substr_count($b,("".$i));
}
if ($numsCountA == $numsCountB) {
return true;
}
return false;
}
function doesMultiplyUpToSix($input)
{
for ($i=2; $i <= 6; $i++) {
$checkStr = "".$input;
$multiple = ($input*$i);
if (!isPermutation($checkStr,$multiple)) {
return false;
}
}
return true;
}
$result=0;
$i=125874;
while ($result==0) {
if (doesMultiplyUpToSix($i)) {
$result=$i;
break;
}
$i++;
}
$answer = $result;
$endTime = microtime(true);
echo "Answer: ",$answer,"\nTime: ",($endTime - $startTime),"\n";
// Answer: 142857
// Time: 0.26s