-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem005.clj
21 lines (16 loc) · 869 Bytes
/
problem005.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
;; 2520 is the smallest number that can be divided by each of the
;; numbers from 1 to 10 without any remainder.
;; What is the smallest number that is evenly divisible by all of the
;; numbers from 1 to 20?
;; first the brute force way (caution, this can take a very long time):
(defn divides? [seq x]
"Takes a number and a seq of divisors and returns wether or not the
number is evenly divisible by each value in the seq"
(every? #(= 0 (mod x %)) seq))
(defn smallest-divisible-by [seq]
(first (drop-while (complement (partial divides? seq)) (iterate inc 1))))
(smallest-divisible-by (range 2 21))
;; a better approach is to realize that we just need to find the least
;; common multiple of the set of integers {2,...,20} and implement a
;; suitable algorithm to calculate that. See
;; http://en.wikipedia.org/wiki/Least_common_multiple for more info.