Smallest Common Multiple | Intermediate Scripting

Hi,

I am stuck at this problem for a while now. I am not sure how to move forward.

function  collectNumsInArray(arr) {
    let arr1 = []
for(let i = Math.min(...arr); i <= Math.max(...arr); i++){
     arr1.push(i);   
   }
   return arr1;
 } 

function sumCommonsNums(arr){
let arr1 = collectNumsInArray(arr);
let lcm = 1;
console.log(arr1)
for(let i = 2; i <= arr1[arr1.length - 1]; i++){
  for(let j = 0; j <= arr1.length; j++){
    if (arr1[j] % i === 0){
        arr1[j] = arr1[j] / i;
        i = i;
 } 
   lcm *= i;
   console.log(lcm)
}  
}
}
sumCommonsNums([1, 5])

what i want to do is keep looping till the i % j for all items in the array === 1. But that’s not happening.

problem link - https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

Thanks,
Deeti

That sounds like a while loop, not a for loop

@JeremyLT thanks, but now, while loop is changing the elements in array one by one. How can I divide all elements by the number, until all the elements are-
arr[j] % i !== 1.

function collectNumsInArray(arr){
     let arr1 = []
for(let i = Math.min(...arr); i <= Math.max(...arr); i++){
      arr1.push(i);   
    }
    return arr1;
  } 


function sumCommonsNums(arr){
 let arr1 = collectNumsInArray(arr);
 let lcm = 1;
 console.log(arr1)
 for(let i = 2; i <= arr1[arr1.length - 1]; i++){
   for(let j = 0; j <= arr1.length; j++){
     while (arr1[j] % i === 0){
         arr1[j] = arr1[j] / i;
         console.log(arr1)
         lcm *= i;
         console.log(i)
  }   
  
 } 
}
}
sumCommonsNums([1, 5])

Thanks again for your help :star2: !!

Let me clarify:
in this loop:

You want to check for every j what exactly?
arr1[j] % i ===0 - this?
And if the above is true for every j, only then you want to execute accumulation like below?

lcm *= i;

That’s the goal or I misunderstood?

1 Like

Hi Thanks for replying,
So I want the function to be like this -
check for elements – elements % === 0.
Divide those elements by that number.
Accumulate
Then check again for that number until elements % !== 0.
and repeat.

That’s how I have been calculating LCM-
example - range 1 to 5
2 | 1 2 3 4 5
2 | 1 1 3 2 5
3 | 1 1 3 1 5
5 | 1 1 1 1 5
| 1 1 1 1 1

LCM 2 * 2* 3 * 5 = 60

This is the method, I am trying to translate to code.

1 Like

That’s interesting. The thing is: I solved this problem literally an hour ago, but I used slightly different math.
I will try to implement your method, and if succeed, I will be able to provide some hints for you.
As I see it now, I will use methods like

UPDATE
Array.prototype.filter() also can be useful

Thank you so much. Yea, I think there are lots of ways to find out LCM. The solution 1, I thinks calculates two elements in the range and then move on to the next one which is interesting but I really want to solve it by the method I am trying.

Thanks again, :star2:

okay, let me try them.

Thanks!!

It looks like you may be mixing up what everything means?

I only see the LCM calculated once, but all of the solutions I’m aware of need the LCM computed many times.

Once in that way when it finds its multiple when it has searched the entire list and divided it by itself.

That’s how I have been calculating LCM-
example - range 1 to 5
2 | 1 2 3 4 5
2 | 1 1 3 2 5
3 | 1 1 3 1 5
5 | 1 1 1 1 5
| 1 1 1 1 1

LCM 2 * 2* 3 * 5 = 60

Two issues

  1. simply multiplying a single running LCM variable by every factor of every number in the range is going to lead to too many factors.

  2. your for loops seem to be backwards from each other?

Like I said, something is jumbled together about your algorithm

  1. simply multiplying a single running LCM variable by every factor of every number in the range is going to lead to too many factors.

That’s the problem. I keep having multiple factors.

  1. your for loops seem to be backwards from each other?

for i, it is running from 2 to the max number in the arr1. for j from arr[0] to the last element.

I can’t figure out what is jumbled. Clearly, i am not able to figure out how to translate the math algorithm into computer algorithm .

If you trying to do a prime factorization of each number in the range, then you will need to keep a separate total of how many times each factor appears. I don’t understand this chart, so I’m not really sure what you see as the mathematical algorithm you are trying to replicate.

we have i === 2 and array [1, 2, 3, 4, 5]


if at least one element in array is evenly divisible by i===2 we need to:

accumulate lcm

change an array like [1, 2, 3, 4, 5] >>> [1, 1, 3, 2, 5] meaning divide by 2 every elem if it’s divided by 2 evenly


The block above need to be repeated until every elem is not divisible by 2 evenly then we move to the next i(looping through i - i guess it should be inner *for* loop)
The whole thing must be stopped when every element in array === 1(i guess it should be condition for outer *while* loop)

BTW I am not really big fan of this approach, feels like too many loop inside the loop inside the loop things. But it’s interesting to implement it

1 Like

I am using division method.

That table should come with some sort of description of how it is actually created.

We loop back to this

Your steps are swapped around, giving you a very large value for the LCM.

I would start by writing out in your own words the steps needed to create this algorithm.

Let me make sure, that I also understood.
This loop

should not be the outer loop, right? I also think so.

There is a way to make it be the outer loop, but there is also a prime factorization approach where this is an inner loop.

1 Like