Created
November 13, 2009 10:06
-
-
Save anonymous/233726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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