Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function gcd(a, b) { //greatest common divisor
- if(isNaN(a) || isNaN(b)){
- //throw new Error("a or b should be a number");
- //console.log('a or b should be a number');
- return false;
- }
- if(Math.floor(a) !== a || Math.floor(b) !== b){
- //throw new Error("a or b should be an integer");
- //console.log('a or b should be an integer');
- return false;
- }
- while(b !== 0){
- let r = a % b;
- a = b;
- b = r;
- }
- return (a<0) ? -a : a;
- }
- function lcm(a, b) { //least common multiple
- return Math.abs(a)*Math.abs(b)/ gcd(a, b);
- }
- function smallestMult(n) {
- let myLcm = 1;
- let i;
- for(i=2; i<=n; i++){
- myLcm = lcm(myLcm, i);
- }
- console.log(myLcm);
- return myLcm;
- }
- smallestMult(20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement