**Find the smallest number divisible by each of the numbers 1 to 20**

The requirement is to find the Lowest Common Multiple (LCM)of the numbers 1 to 20

We can very easily find the LCM using the Greatest Common Divisor (GCD) using the formula

lcm(x, y) = (x * y) / gcd(x, y)

A very fast method to calculate GCD is using Euclid’s algorithm

gcd(x, y) = gcd(y, x mod y)

Implementing this in Clojure

(defn gcd [x y]
(cond
(zero? x) y
(zero? y) x
:else (recur y (mod x y))))
(defn lcm [x y]
(/ (* x y) (gcd x y)))
(reduce lcm (range 1 21))

*gcd* calculates the greatest common divisor of 2 integers

(gcd 6 8 )
=> 2

*lcm* calculates the lowest common multiple of 2 integers

(lcm 6 8 )
=> 24

### Like this:

Like Loading...

*Related*

## Leave a Reply