Skip to content

Instantly share code, notes, and snippets.

Created November 13, 2009 10:06
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 anonymous/233726 to your computer and use it in GitHub Desktop.
Save anonymous/233726 to your computer and use it in GitHub Desktop.
;; project euler problem 12
(use
'clojure.contrib.math
'clojure.contrib.lazy-seqs
'clojure.contrib.combinatorics)
(def triangles
(rest (map first (iterate (fn [[a b]] [(+ a b) (inc b)]) [0 1]))))
(defn factor-of? [a b]
(= 0 (mod a b)))
(defn smallest [xs]
(first (sort xs)))
(defn exp-range-max [n factors]
(/ n (smallest factors)))
(defn prime-factors-of [n]
(filter #(factor-of? n %) (take-while #(< % n) primes)))
(defn raise-nums [nums exps]
(map (fn [[b e]] (expt b e)) (partition 2 (interleave nums exps))))
(defn exp-combo-works? [n factors powers]
(= n (apply * (raise-nums factors powers))))
(defn possible-powers [n factors]
(range 1 (inc (exp-range-max n factors))))
(defn find-exp-combo [n factors]
(first (drop-while #(not (exp-combo-works? n factors %))
(selections (possible-powers n factors) (count factors)))))
(defn num-divisors [n]
(let [combo (find-exp-combo n (prime-factors-of n))]
(apply * (map inc combo))))
(num-divisors 36) ;should be 9
(take 10 (map num-divisors triangles));brokt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment