Problem 5 (yes these are pretty quick ones) from
Project Euler is a really easy one again:
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?
I wrote a simple loop to increment a variable until it found a value that's division by 11,12,13,14,15,16,17,18,19 and 20 all resulted in whole numbers.
There was no need to test numbers between 1 and 10 since the numbers between 11 and 20 are all multiples of those anyway:
<?
/******************************************************************
* ProjectEuler.net * Problem 5 * 2010-05-31 * Martin Caine *
******************************************************************/
$i = 1;
while( !(( ($i/11) == (int)($i/11) ) &&
( ($i/12) == (int)($i/12) ) &&
( ($i/13) == (int)($i/13) ) &&
( ($i/14) == (int)($i/14) ) &&
( ($i/15) == (int)($i/15) ) &&
( ($i/16) == (int)($i/16) ) &&
( ($i/17) == (int)($i/17) ) &&
( ($i/18) == (int)($i/18) ) &&
( ($i/19) == (int)($i/19) ) &&
( ($i/20) == (int)($i/20) )) )
{
$i++;
}
echo $i;
?>
If you found this post helpful please leave a comment below: