Is this considered cheating?

I’m doing the Smallest Common Multiple problem. I’ve tried different logics, but nothing worked. I ended up creating one huge array of numbers to compare against the given array. The performance seems to be abysmal, but I’m wondering if this can be considered valid.

function smallestCommons(arr) {
  let hugeNumber = 6056821;
  let hugeArray = [];
  let newArr = [];
  let start = Math.min(...arr) // in case arr isn't sorted, populate newArr starting from lowest number using the first loop
  let end = Math.max(...arr)
  arr = newArr // now arr is [18, 19, 20, 21, 22, 23]
    for (let i = start; i <= end; i++) {
        newArr.push(i)
    }
  for (let i = 1; i <= hugeNumber; i++) { // populate hugeArray
      hugeArray.push(i)
  }
  for (let n of hugeArray) { // retrieve the number from hugeArray that is true for all numbers in arr
    if (arr.every((a) => n % a === 0)) {
      return n;
    }
  }
}

console.log(smallestCommons([23, 18]))```
1 Like

Cheating? No. You got a solution that functions. That’s always a good first step.

That said, this really isn’t a great solution, which you knew. So, lets think about why this isn’t a great solution.

Making large arrays in Javascript is always slow. Is this large array needed?

You are only creating the array to loop over its contents once, so no, you could just use that for loop you used to make the array instead.

So lets start there. Can you remove the huge array?

2 Likes

In code, if it gives you the right answer, I wouldn’t call it cheating.
Is it good practice though… egh… no… for a few reasons.

First, memory. As you said, it’s a HUGE array. That’s a lot of memory being used up to store some numbers… that are in order anyway. Why not just loop from 0 to hugeNumber and check each one?

Second, speed. Just some quick logic, if one of the numbers in arr is 5, than only every fifth number could possibly by the lowest common multiple. So why check every single number?

Third, reliability. What happens if the array I give you contains hugeNumber + 1?

Just a few points to think about. :slightly_smiling_face:

2 Likes

I guess I could remove it, but I’m not sure how. Can I get a hint? xD

EDIT: just figured it out, looks a bit better now

function smallestCommons(arr) {
  let hugeNumber = 100000000000;
  let newArr = [];
  let start = Math.min(...arr); // in case arr isn't sorted, populate newArr starting from lowest number using the first loop
  let end = Math.max(...arr);
  arr = newArr // now arr is [18, 19, 20, 21, 22, 23]
    for (let i = start; i <= end; i++) {
        newArr.push(i)
    }
  for (let i = 1; i <= hugeNumber; i++) { // loop through hugeNumber until the value is found
    if (arr.every((a) => i % a === 0)) {
      return i;
    }
  }
}
1 Like

Thank you, I edited the code. Now there are only 2 issues left :sweat_smile:

I still see hugeArary in there though? Where’s your code without it?

My bad, pasted the wrong thing. In this code above, if I set hugeNumber to Infinity it works. So is that an optimal enough solution?

There are two clever things you can do from here.

Bit of a tricky question for you - what is the absolute smallest number that might work as a multiple of all numbers from start to stop. Think super small examples here. What if the range is [1, 2]?

1 Like

Hmm…in this case it’s 2, but I’m not seeing any patterns.

That’s enough of a pattern. If we get super duper lucky, we could have end work. So where should your loop start. And what should it increment by?

Honestly no idea, I doubt I can answer right now. Can you pls just reveal the answer? xD

Just take the questions one at time. If end is the smallest number it might be, why start looking at 1?

Ahh…I should change i = 1 to i = end, right?

Yup. That is a small fix. And a bigger fix is adjusting your loop to only look at multiples of end.

Well, that’s beyond my comprehension right now :sweat_smile:
Thank you for your help. And setting hugeNumber to Infinity is also a fix, right? That way all numbers are covered.

Ehhhhh, its not really an ideomatic fix. It would be clearer to use a while loop.

Could you please show me how?

Just look at the solutions if you don’t want to try to write it yourself:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.