LCM Algorithm Seems Pretty Intense

LCM Algorithm Seems Pretty Intense


I just finished the “Smallest Common Multiple” problem in the Front-End “Intermediate Algorithm Scripting” section and I’m just wonder, did this problem seem really involved to anyone else? My solution required using 11 different functions and ran almost 150 lines long. This one seemed a lot more intense than any of the previous problems in this section and took me a few hours to solve. Not that I’m complaining about the workload or complexity; I’m just curious if there was an easier way of solving this problem (that isn’t massively resource inefficient). I’ll post a CodePen link to my solution below. Note: I’m using a trick with prime factorization to solve the problem which I’ll post a link to below as well.

My Solution
Trick for Solving LCM Problems Using Prime Factorization


I realized that the LCM of three or more numbers is equal to the LCM of the first two numbers, then the LCM of that result and the third number, and so on.

LCM(a, b, c) == LCM(LCM(a, b), c)

That means I could just create a simple lcm function that takes two arguments, then create an array of numbers out of the given bounds, and do a reduce on that array using the lcm function.


Oh boy. I feel dumb. I knew I was going way overboard. I’ve amended a new solution to my previous codePen using your method. It’s much shorter and only took a fraction of the time to solution. Damnit XD