I just finished the Small Common Multiple challenge, but I don’t think my choice of algorithm was the best. I’d like to know how others approached this one.

I’ve been doing a lot of study into functional programming recently and I decided that I wanted to try to solve this one with a purely functional approach. To this end, the table method listed here seemed like a good choice, but it hasn’t worked out so well.

The first version of my submission had several recursive helper functions and worked fine for smaller values, but I quickly ran into stack overflow errors (I blame JavaScript’s lack of tail recursion optimisation, my code was fine), so I had to redesign the main looping function as a while() loop. Even here there was a problem as then the site complained about an infinite loop. Investigating this, I discovered that number of iterations of while() loop using the table algorithm escalates quickly. With the input value of [1, 11] the loop clocks up a “mere” 21,969 iterations, but this jumps to a whopping 291,239 iterations at [1, 13], an eye-watering 10,040,399 for [1, 17] and I’m still waiting for [1,19] to finish. I’m now thinking that my choice of algorithm may not have been the best.

I’ve passed the test, the code works, but that seems to be a dreadfully ineffecient algorithm. I’d like to know how other people approached this problem and what algorithm they choose. Did you run into the same problems, or is there a better way of solving this one?