Created
September 18, 2013 09:15
-
-
Save vasco3/6606639 to your computer and use it in GitHub Desktop.
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?
This file contains 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
/* | |
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? | |
*/ | |
// determine start number = n ie. 20 | |
function getSmallestDivisor(n){ | |
var i = n; | |
// if not 0 then i++ | |
function checkI(i){ | |
for (var j = 1; j <= n; j++) { | |
// loop 20 % i where i goes from 1 to 20 must % = 0 | |
if( i % j !== 0 ){ return false; } | |
}; | |
return true; | |
} | |
// break loop when n all are 0 | |
while( !(checkI(i)) ){ i+=n; } | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
232792560