Here’s the link to the problem description. My first try at the problem resulted in a slow but working implementation.
(defn multiple-of-all? [n factors] (every? #(zero? (rem n %)) factors)) (defn least-common-multiple [nums] (let [ns (sort > nums) mx (first ns)] (first (filter #(multiple-of-all? % ns) (iterate #(+ mx %) mx))))) (least-common-multiple (range 2 21))
An elegant three-line solution submitted anonymously was my favorite, and is very fast:
(defn gcd [a b] (if (zero? b) a (recur b (mod a b)))) (defn lcm [a b] (/ (* a b) (gcd a b))) (reduce #(lcm %1 %2) (range 1 21))
There are three clever things about this that I like:
- Using recur in
gcdleads to a simple, efficient definition.
- No functions use variable arity or require collections as arguments. Simple.
reduceis the right idiom for “multiplication-like” algorithms such as this.
Once again, it seems it is useful to check if a problem easily fits into the