Skip to content

Instantly share code, notes, and snippets.

@lypanov
Created February 6, 2010 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lypanov/c6791d30086488f4335c to your computer and use it in GitHub Desktop.
Save lypanov/c6791d30086488f4335c to your computer and use it in GitHub Desktop.
(defn divide-if-remainderless [a b]
(if (zero? (mod a b)) (/ a b) a ))
(defn divide-table-while-integer
([table divisor]
(divide-table-while-integer table divisor 0))
([table divisor n]
(let [divided-table (map #(divide-if-remainderless % divisor) table)]
(if (= table divided-table) [table n]
(recur divided-table divisor (inc n))))))
(defn lcm
([table]
(lcm table 1 [2 3 5 7 9 11 13 17 19]))
([table result primes]
(let [prime (first primes)
[updated-table num-divisions] (divide-table-while-integer table prime)
new-lcm (apply * (cons result (repeat num-divisions prime)))]
(if-let [rest-primes (next primes)] (lcm updated-table new-lcm rest-primes)
new-lcm))))
(lcm (range 1 11))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment