Help needed in smallest common multiple

Help needed in smallest common multiple
0

#1

function smallestCommons(arr) {
  var array=[];
  var mul=[];
  var max=Math.max(arr[0],arr[1]);
  var min=Math.min(arr[0],arr[1]);
  for(i=max;i>=min;i--){
    array.push(i);
    }
  
 for(i=0;i<array.length;i++)
 {
  for(j=1;j<=20;j++)
  {
  mul.push(array[i]*j);
    
  } 
  
  }
  
  
  
return mul;
}


smallestCommons([1,5]);

here is my code for smallest common multiple. i am getting the multiplication of every number in array up to 20. But i don’t know how to return the common number from them. Help Me.


#2

Hi. Getting the first n multiples of each number (in this case, 20) is not a viable way of getting the smallest common multiple of numbers. For instance, the LCM for numbers 1 to 5 is 60, but you won’t find 60 in the first 20 multiples of 1 and 2.

One solution is to create a function that returns the LCM of two numbers (call it lcm(a, b) or something). Then get the LCM of the first two numbers, then the LCM of that result and the third number, and so on. .reduce() is perfect for this approach.

function smallestCommons(arr) {
  var array = [];
  var mul = [];
  var max = Math.max(arr[0], arr[1]);
  var min = Math.min(arr[0], arr[1]);
  for(i = max; i >= min; i--) {
    array.push(i);
  }

  return arr.reduce(function(a, b) {
    // I'll leave it to you to create the LCM function.
    return lcm(a, b);
  })

  // alternatively,
  // return arr.reduce(lcm);
}

#3

i need to create lcm within arr.reduce function?


#4

but still can you tell me any way by which i can return the common number from the array?


#5

You can create it within .reduce(), like

return arr.reduce(function lcm(a, b) {
   // code for computing lcm...
});

#6

okay i got it… and the way i am doing can you tell me the way to get common number from the array?


#7

Hmmm… if you insist, one way I came up with on top of my head is to sort the array, then get the number with duplicates. But the thing is there will most likely be more than one number that has duplicates. In your code, 2 has duplicates (1 x 2, and 2 x 1), 4 has duplicates (1 x 4, 2 x 2, 4 x 1), and so on. You still have no idea which one is the smallest multiple for all numbers in question.


#8

I did using the LCM numbers array approach. Take a look:

function isPrime(num) {
    var prime = true,
        div = 2;

    while (prime === true && div !== num) {
        if (num % div === 0) {
            prime = false;
        }
        div++;
    }

    return prime;
}

function getSequencialNumbers(start, end) {
    var numbers = [];

    for (var i = start; i <= end; i++) {
        numbers.push(i);
    }

    return numbers;
}

function getLCM(numbers) {
    var found = false,
        count,
        notDivisible,
        divisor = 2,
        multiples = [],
        lcm;

    while (!found) {
        if (!isPrime(divisor)) {
            divisor++;
            continue;
        }

        count = 0;
        notDivisible = 0;
        numbers = numbers.map(function(val) {
            if (val === 1) {
                count++;
            }

            if (val % divisor === 0) {
                return val / divisor;
            } else {
                notDivisible++;
            }

            return val;
        });

        if (count === numbers.length) {
            found = true;
        } else if (notDivisible === numbers.length) {
            divisor++;
        } else {
            multiples.push(divisor);
        }
    }

    lcm = multiples.reduce(function(prev, curr) {
        return prev * curr;
    });

    return lcm;
}

function smallestCommons(arr) {
    var start = Math.min.apply(null, arr),
        end = Math.max.apply(null, arr),
        divisors = getSequencialNumbers(start, end);

    return getLCM(divisors);
}

smallestCommons([1, 5]);

`


#9

thank you for the help. Task completed!!


#10

Maybe you can rewrite the isPrime function as

function isPrime(num) {
  // or replace `var` with `let` on ES6.
  // You can also change the condition to `div <= Math.sqrt(num)`
  // to significantly cut the number of iterations.
  for (var div = 2; div !== num; div++) {
    if (num % div === 0) {
      return false;
    }
  }
  return true;
}