Skip to content

Instantly share code, notes, and snippets.

@hroi
Created February 23, 2010 15:26
Show Gist options
  • Save hroi/312296 to your computer and use it in GitHub Desktop.
Save hroi/312296 to your computer and use it in GitHub Desktop.
; Problem 5
; 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?
(defn divisible-by? [x y]
(zero? (mod y x)))
(defn find-sol* [n startfrom]
(let [factors (vec (reverse (range (int (/ (inc n) 2)) (+ 1 n))))
mingcd (* n (int (/ startfrom n)))
start (bigint (if (zero? mingcd) n mingcd))]
(loop [sofar start]
(cond (every? #(divisible-by? % sofar) factors)
sofar
:else
(recur (+ n sofar))))))
(defn find-sol [n]
(reduce #(find-sol* %2 %1) 2 (range 2 (inc n))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment