-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob-017.clj
23 lines (21 loc) · 968 Bytes
/
prob-017.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
;;;;;;;;;; problem 017 ;;;;;;;;;;
(def one-nineteen ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"])
(def twenty-by-10s ["twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
"eighty", "ninety"])
(defn num-to-english
([n] (num-to-english n ""))
([n s]
(cond (zero? n) s
(< n 20) (str s (one-nineteen (dec n)))
(< n 100) (recur (mod n 10)
(str s (twenty-by-10s (- (quot n 10) 2))))
(< n 1000) (recur (mod n 100)
(str s (one-nineteen (dec (quot n 100)))
"hundered" (if (zero? (mod n 100))
"" "and")))
(= n 1000) "onethousand")))
(defn prob-017 []
(reduce + (map #(count (num-to-english %)) (range 1 1001))))