Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Solved 16 and 20

  • Loading branch information...
commit 0c234d09ff603ffedef4b8b869edea62488af025 1 parent 7a15874
@bobbyno authored
View
2  src/euler/16.clj
@@ -5,5 +5,3 @@
[n]
(.toPlainString (BigDecimal. (Math/pow 2 n))))
-(defn sum-digits [n]
- (reduce + (map #(Integer/parseInt (str %)) (seq (two-str n)))))
View
12 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))))
View
5 src/euler/common.clj
@@ -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))))
View
4 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 []
@@ -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))))
View
5 test/euler/test/16.clj
@@ -1,5 +1,6 @@
(ns euler.test.16
(:use [euler.16]
+ [euler.common]
[clojure.test]))
(deftest test-two-str
@@ -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))))
)
View
2  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
View
15 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)))
+)
View
10 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)))
+)
Please sign in to comment.
Something went wrong with that request. Please try again.