Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Problem 12

  • Loading branch information...
commit 376a4e21fa4361df34c931c643014b6d3c932c7a 1 parent 22175d1
@bobbyno authored
Showing with 27 additions and 0 deletions.
  1. +17 −0 src/euler/12.clj
  2. +10 −0 test/euler/test/12.clj
View
17 src/euler/12.clj
@@ -0,0 +1,17 @@
+(ns euler.12
+ (:use euler.common))
+
+(def triangle-series
+ (map #(arithmetic-series %) (iterate inc 1)))
+
+(defn divisors [n]
+ (letfn [(calc [k n limit results]
+ (if (>= k limit)
+ results
+ (if (zero? (rem n k))
+ (recur (bigint (inc k)) (bigint n) (bigint (/ n k)) (conj results k (bigint (/ n k))))
+ (recur (bigint (inc k)) (bigint n) limit results))))]
+ (sort (distinct (calc 2 (bigint n) (bigint n) [1 (bigint n)])))))
+
+(defn first-triangle-with-div-under [n]
+ (int (first (drop-while #(> (bigint n) (count (divisors %))) triangle-series))))
View
10 test/euler/test/12.clj
@@ -0,0 +1,10 @@
+(ns euler.test.12
+ (:use euler.12
+ clojure.test))
+
+; TODO: Convert to the optimized version described in Euler
+(deftest test-first-triangle-with-div-under
+ (is (= 28 (first-triangle-with-div-under 5)))
+ (is (= 528 (first-triangle-with-div-under 20)))
+ (is (= 2162160 (first-triangle-with-div-under 300)))
+)
Please sign in to comment.
Something went wrong with that request. Please try again.