-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path62.php
50 lines (48 loc) · 1.6 KB
/
62.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
<!--
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
-->
<?php $startTime = microtime(true);
function isCubic($input) {
$number = pow($input,1/3);
if (floor($number) == $number)
{
return true;
}
return false;
}
$cubeArray = array();
for ($i=5000; $i <= 10000; $i++) { //first checked 1->5000
$cubeArray[] = pow($i,3);
}
$cubeArrayCount = count($cubeArray);
for ($i=0; $i < $cubeArrayCount; $i++) {
for ($j=0; $j <= 9; $j++) {
$numsArray[$j] = substr_count($cubeArray[$i],$j);
}
$permCubeCount = 0;
for ($j=0; $j < $cubeArrayCount; $j++) {
if ($numsArray[0] == substr_count($cubeArray[$j],"0")
&& $numsArray[1] == substr_count($cubeArray[$j],"1")
&& $numsArray[2] == substr_count($cubeArray[$j],"2")
&& $numsArray[3] == substr_count($cubeArray[$j],"3")
&& $numsArray[4] == substr_count($cubeArray[$j],"4")
&& $numsArray[5] == substr_count($cubeArray[$j],"5")
&& $numsArray[6] == substr_count($cubeArray[$j],"6")
&& $numsArray[7] == substr_count($cubeArray[$j],"7")
&& $numsArray[8] == substr_count($cubeArray[$j],"8")
&& $numsArray[9] == substr_count($cubeArray[$j],"9")
) {
$permCubeCount++;
}
}
if ($permCubeCount==5) {
$firstFivePer = $cubeArray[$i];
break;
}
}
$answer = $firstFivePer;
$endTime = microtime(true);
echo "Answer: ",$answer,"\nTime: ",($endTime - $startTime),"\n";
// Answer: 127035954683
// Time: 0.23s