0

Find the least common multiple of the provided parameters using Table Method that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. There will only be two parameters. For ex [1,3], find the lcm of 1,2,3.

Note - It might create an infinite loop

function smallestCommons(arr) {

  var nums = [];
  var multiples = [];
  if(arr[0]>arr[1]) {
    var bigger = arr[0];
  } else {
    var bigger = arr[1];
  }

  for(var i=bigger;i>0;i--) {
    nums.push(i);
    console.log(i);
  }console.log(nums + " nums");

  var sums = 0;

  while(sums != nums.length) {
    for(var k=0;k<nums.length;k++) {
      if(nums[k] % 2 === 0) {
        nums[k] = nums[k]/2;
        multiples.push(2); 
      } else if(nums[k] % 3 === 0) {
        nums[k] = nums[k]/3;
        multiples.push(3); 
      }else if(nums[k] % 5 === 0) {
        nums[k] = nums[k]/5;
        multiples.push(5); 
      }else if(nums[k] % 7 === 0) {
        nums[k] = nums[k]/7;
        multiples.push(7); 
      }else if(nums[k] === 1) {
        break;
      }else {
        nums[k] = nums[k]/nums[k];
        multiples.push(nums[k]);
      }
    }
  for(var j = bigger; j>0;j--) {
     sums = sums + nums[j]; 
   }
  }
 var scm = [multiples].reduce(function(a,b){console.log(a*b)}); return scm
}
smallestCommons([1,5]);
Lavios
  • 1,049
  • 9
  • 21
  • What is your actual requirement? – Ajmal E S Oct 19 '15 at 06:52
  • I want it to first divide the number by the smallest prime number (2), if evenly divisible push it to multiples, if not, move on to the next prime number and so on... At the last multiply the multiples with itself and thus resulting in the answer(lcm) – Lavios Oct 19 '15 at 06:55
  • nums[k] = nums[k]/nums[k]; What does this line give you? – Ajmal E S Oct 19 '15 at 06:56
  • Divide the number by the prime numbers till 7, and if it is still not evenly divisible, then just make it a 1. I just wanted to do it first with the small numbers but I can't get it to work. – Lavios Oct 19 '15 at 06:59

2 Answers2

1

What you need is find the LCM in range (n, m) ?

Finding least common multiples by prime factorization seems better. You can use Legendre's formula to find all prime factors of n! and m! , then just do a simple subtraction.

throwit
  • 704
  • 3
  • 13
1

I found this to be a simple solution, It works wonders;

  • Loop through all possible numbers, beginning with lower bound input (var i)
  • for every number, test divisibility by each number between and including input bounds (var j)
  • if i meets all criteria return it as answer, otherwise increment i by 1 and try again

click here for explanation of ? operator in variable initialization

function smallestCommons(arr) {


  //set variables for upper and lower bounds 
  //incase they aren't entered in ascending order
  var big = arr[0] < arr[1] ? arr[1]:arr[0],
      small = arr[0] < arr[1] ? arr[0]:arr[1],
      i = small;

  //loop through all numbers, note the possibility of an infinite loop
  while(true){


      //test each number for divisibility by by both upper and lower 
      //bounds, as well as by all sequential numbers inbetween
      for(var j = small; j <= big; j++){

        if(i % j === 0){
          if(j===big){
            return i;
          }
        }else {
          break;
        }
      }   
    i++;
  }
}


smallestCommons([1,5]);  //60
Community
  • 1
  • 1