Faster More Efficient Solution to Smallest Common Multiple?

Hi all. As I’m learning throughout solving these challenges is that sometimes even though I am getting confident in solving these algorithms - in the real world, my solutions will be slow. So I’d appreciate it if anybody can help guide me to creating algorithms that will process faster! Thanks in advance. :blush:

Link to test.

MY FIRST SOLUTION (though isn’t accepted by FCC for looping limit or something)

//SHORTER SOLUTION BUT SLOWER
function smallestCommons(arr) {
  //CREATE THE ARRAY OF DIVISIBLE NUMBERS
  arr = arr.sort((x, y) => x - y);
  let divisibleBy = [];
  for (let i = arr[0]; i < (arr[1]); i++) {
    divisibleBy.push(i);
  }
  //FIND IF J IS DIVISIBLE
  let num = 0;
  for (let j = 1; j; j++) {
    if (j % arr[1] === 0) {
      if (divisibleBy.every(elem => j % elem === 0)) {
        num = j;
      break;
      }
    }
  }
  return num;
}

MY SECOND SOLUTION (accepted by FCC but is just a slight cheeky tweak to the first)

function smallestCommons(arr) {
  //CREATE THE ARRAY OF DIVISIBLE NUMBERS
  arr = arr.sort((x, y) => x - y);
  let divisibleBy = [];
  for (let i = arr[0]; i < (arr[1]); i++) {
    divisibleBy.push(i);
  }
  //FIND IF J IS DIVISIBLE
  let num = 0;
  for (let j = 1; j; j++) {
    if (divisibleBy.every(elem => j % elem === 0)) {
        num = j;
      break;
    }
  }
  //EXTRA STEP FOR FASTER FUNCTION
  if (num % arr[1] === 0) {
    num;
  } else if (num % arr[1] !== 0) {
    num *= arr[1];
  }
  return num;
}

What you are doing is called brute force approach where you are going through each number to find what you are looking for. This is always the slowest approach. My recommendation is first do a search to find out if it is a simple mathematical problem & there are algorithms already defined to solve it. Then try to implement that algorithm using your own logic. For example, this is a known mathematical problem called finding LCM - least common multiple. Do a search for that term and you will see some explanation on how to find a solution for this issue then develop your solution

1 Like

Ah, so I should refrain from using brute force from now on if possible. Hmm… I did look up the formula for it but at the time I had no idea how to turn it into code. Although, I somehow solved the orbital period one in the later tests. I guess this is something I just have to start practicing to begin with. Thank you very much for your input! Just out of curiosity… How often are mathematical formulas applied in real life programming jobs? And do you recommend retaking some math classes? :sweat_smile:

I have tried the prime factors thing, I don’t know if it is me not knowing enough code or what, but it is not efficient enough… so I just brute forced it in an other way, just with some math knowledge.

When you have Algorithms to solve, knowing what primes are and how to find them is a good thing. (Hint number 1)

What is a minimum common multiple? What is the relation with the numbers and with primes?
And, if finding the prime factors of each number, and choosing the right ones to use may require complex logic and too many steps…
… could you use division instead of just multiplication? (Hint number 2)

Anyway, if you want to review math, I advice Khan Academy

And, you know, if you find an other way to solve this, go for it, you don’t have to follow hints that don’t work for you

1 Like

Thanks for the tip! I can picture what you’re trying to get at. I’ll definitely try your method when I revisit this challenge some day. And thank you for reminding me about Khan Academy!