Created
November 19, 2013 07:01
-
-
Save pmgupte/7541362 to your computer and use it in GitHub Desktop.
PHP solution to Project Euler problem # 5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 5: Smallest multiple | |
* | |
* 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? | |
*/ | |
$evenlyDivisible = false; | |
$limit = 20; | |
for($i=$limit; !$evenlyDivisible; $i+=$limit) { | |
for($j=2; $j<$limit; $j++) { | |
if($i % $j == 0) { | |
$evenlyDivisible = true; | |
} else { | |
$evenlyDivisible = false; | |
break 1; // break inner loop | |
} | |
} | |
if($evenlyDivisible) { | |
echo "smallest number evenly divisible by 1..$limit is $i\n"; | |
break 1; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment