Skip to content

Commit

Permalink
Solved 16 and 20
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyno committed Jan 8, 2012
1 parent 7a15874 commit 0c234d0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/euler/16.clj
Expand Up @@ -5,5 +5,3 @@
[n]
(.toPlainString (BigDecimal. (Math/pow 2 n))))

(defn sum-digits [n]
(reduce + (map #(Integer/parseInt (str %)) (seq (two-str n)))))
12 changes: 12 additions & 0 deletions src/euler/20.clj
@@ -0,0 +1,12 @@
(ns euler.20
(:use [euler.common]))

(defn ! [x]
(letfn [(factorial [x product]
(if (zero? x)
product
(recur (dec x) (* x product))))]
(factorial (bigint x) 1)))

(defn sum-! [x]
(sum-digits (str (! x))))
5 changes: 4 additions & 1 deletion src/euler/common.clj
Expand Up @@ -3,4 +3,7 @@

(defn arithmetic-series [n]
(if (< n 0) (throw (ArithmeticException. "only supports natural numbers")))
(* n (mean [n 1])))
(* n (mean [n 1])))

(defn sum-digits [n]
(reduce + (map #(Integer/parseInt (str %)) (seq n))))
4 changes: 3 additions & 1 deletion src/euler/2.clj → src/euler/fibs.clj
@@ -1,4 +1,4 @@
(ns euler.2)
(ns euler.fibs)

; from Programming Clojure, p. 157
(defn fibonacci-seq []
Expand All @@ -16,3 +16,5 @@
(defn fibonacci [k]
(last (take k (fibonacci-seq))))

(defn fib-with-term-sized [k]
(count (take-while #(< (count (str %)) k) (fibonacci-seq))))
5 changes: 3 additions & 2 deletions test/euler/test/16.clj
@@ -1,5 +1,6 @@
(ns euler.test.16
(:use [euler.16]
[euler.common]
[clojure.test]))

(deftest test-two-str
Expand All @@ -8,6 +9,6 @@
)

(deftest test-sum-digits
(is (= 26 (sum-digits 15)))
(is (= 1366 (sum-digits 1000)))
(is (= 26 (sum-digits (two-str 15))))
(is (= 1366 (sum-digits (two-str 1000))))
)
2 changes: 1 addition & 1 deletion test/euler/test/2.clj
@@ -1,5 +1,5 @@
(ns euler.test.2
(:use [euler.2]
(:use [euler.fibs]
[clojure.test]))

(deftest test-fibonacci-terms
Expand Down
15 changes: 15 additions & 0 deletions test/euler/test/20.clj
@@ -0,0 +1,15 @@
(ns euler.test.20
(:use [clojure.test]
[euler.common]
[euler.20]))

(deftest test-!
(is (= 1 (! 0)))
(is (= 1 (! 1)))
(is (= 6 (! 3)))
)

(deftest test-sum-!
(is (= 27 (sum-! 10)))
(is (= 648 (sum-! 100)))
)
10 changes: 10 additions & 0 deletions test/euler/test/25.clj
@@ -0,0 +1,10 @@
(ns euler.test.25
(:use [clojure.test]
[euler.fibs]))


(deftest test-fib-with-term-sized
(is (= 12 (fib-with-term-sized 3)))
(is (= 45 (fib-with-term-sized 10)))
(is (= 476 (fib-with-term-sized 100)))
)

0 comments on commit 0c234d0

Please sign in to comment.