-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.php
34 lines (32 loc) · 803 Bytes
/
05.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
<!--
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
-->
<?php $startTime = microtime(true);
//Lowest Common factor of two numbers
function LCF($a,$b)
{
$HCDValue = 0;
for ($i = $b; $i >= 1; $i--) {
if ($a%$i==0 && $b%$i==0) {
$HCDValue = $i;
break;
}
}
return $HCDValue;
}
//lowest common multiple of two numbers
function LCM($a,$b)
{
return ($a*$b)/LCF($a,$b);
}
// looping values 2-20
$CurrentNum = 2;
for ($i=3; $i < 21; $i++) {
$CurrentNum = LCM($CurrentNum,$i);
}
$answer = $CurrentNum;
$endTime = microtime(true);
echo "Answer: ",$answer,"\nTime: ",($endTime - $startTime),"\n";
// Answer: 232792560
// Time: 0.00004s