Smallest Common Multiple | Intermediate Scripting

Your steps are swapped around.

I think that part is figured out.

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

function for number Collection in array
-Find minimum and maximum number in the array.
-Create a list of numbers ranging between them. Square brackets [a , b]. with a being min and b max.

function for LCM
-let lcm = 1
-let arr1 = array from the function where we collected an array from range

-start a loop ranging from 2 to the max number in the array. let it be i.
-start a loop again on the inclusive range of array. let it be j

  • if j % 2 === 0
    • then lcm = lcm * 2;
      -arr1[j] = arr1[j] / i

Important note: here we are calculating LCM more times it is required. A method here is needed where I can straight away divide all the elements which are % i === 0.

Then again set i = i until element%i !== 0.

Here, where something is going wrong for sure.

This isn’t really a list of steps. You’re describing the code that you know is wrong in words. Start with the algorithm that you know is good and try to condense it into steps. If you were going to tell a student how to do this by hand with pencil and paper, what would they do?

You’re going to need a while loop as your second loop, I think, but why isn’t clear unless you understand the algorithm.

Currently, you have your while loop for dividing out factors inside of the loop over the range, which means that you are multiplying the 2 in your example above 3 times instead of twice.

let me try that out and see if it works out. Thank you so much for your time again.

You will need to do more that just switch around loops.

Ok I was able to implement… something. I followed pseudocode that I described above.
It’s not looking good:
three nested loops
plus some usage of ‘looping methods’: some() - used twice in the code, reduce() - used once in the code

It’s giving correct results for:
smallestCommons([1, 5])//60
smallestCommons([5, 1])//60
smallestCommons([1, 13])//360360
smallestCommons([23, 18])//6056820

The thing is: for the case
smallestCommons([2, 10]) >>> execution is too slow, it’s crushing
Error: Command failed: timeout

Note: I tested it in online-compilers, not in fcc environment

So yeah, this algo is working, but to implement it efficiently is not the easiest task(for me at least)

I am not sure in this case what is a good algorithm. A solved problem steps could be a good algorithm but here I haven’t solved it yet.

I think by hand and paper too the explanation would be more or less same until i have any breakthrough.

Again, I think, understanding the mathematical logic of the algo is not the issue here, translating it into code is.

Thanks, I will try again!!
Thanks for your time :star2:

To do this by hand you would keep track of these numbers i and j? I would not.

The biggest issue in your code is tow big discrepancies between the pencil and paper algorithm and your loops.

The pencil and paper version goes across a row, but you aren’t doing that. You are removing all factors of 2 from each number in the range instead of removing the first factor of 2 from all numbers in the range and then removing the second factor of two from all numbers in the range, and so on until you run out of factors of two.

Hi @admit8490 , how and why are you using reduce for in the function?

Thanks,
Deeti

Hello again, I am using reduce for this:

I’m not sure that it’s the the best approach btw:

arr1 = arr1.reduce(
    (newarray, item) => 
    item % i === 0 ? [...newarray, item / i] : [...newarray, item], []
    );          
            

My code by the way is very mysterious :upside_down_face: It’s giving right answers for some cases.
And for another cases it’s very slow and giving errors.

I can’t figure out where is the weakness for now

1 Like

Omg Omg Omg I did it finally. Thanks for the some(), I think I solved by that method. I am so happy!!! I will share my code in the message with you. I think we can’t share the code publicly here.

1 Like

Well, we can, theoretically. But for working solutions personal message would be much better option indeed

1 Like

You can share your soluton in a thread after you solve a challenge as long as you use the spoiler tags.

It’s against the rules to give a solution instead of help and to make a thread just to give away a solution.

1 Like

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