context: this is somewhat related to project euler problem 5, which is:
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
I apply prime number decomposition to the range of numbers from 1 to 20, and the product of all that should be the smallest number.
(absorb '(2 2 2) '(2 3 4)) -> '(2 2 2 3 4) ;; does not have to be sorted
(defn absorb [seq1 seq2]
(loop [s1 (sort seq1)
s2 (sort seq2)
acc '()]
(let [s1-curr (first s1)
s2-curr (first s2)]
(cond (and (empty? s1) (empty? s2)) acc
(empty? s1) (into s2 acc)
(empty? s2) (into s1 acc)
(= s1-curr s2-curr) (recur (rest s1) (rest s2) (cons s1-curr acc))
:else (recur (rest s1) s2 (cons s1-curr acc))))))
Is there a way to write this in more idiomatic Clojure, or a more optimized/clea er version?
20
: Calculate the lcm of two numbers as the product divided by the greatest common divisor (gcd). The gcd and hence the lcm are quick to calculate. And lcm distributes just as addition and multiplication do. \$\endgroup\$