-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumbers.clj
More file actions
25 lines (17 loc) · 837 Bytes
/
Copy pathHappyNumbers.clj
File metadata and controls
25 lines (17 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(ns HappyNumbers)
(println (str 16))
(println (map #(Integer/parseInt (str %)) (seq (str 16))))
(defn intToDigits2 [x] (map #(Integer/parseInt (str %)) (seq (str x))))
(defn HappyNumbers [n] (println n) (if (= n 1) true (HappyNumbers (reduce + (map #(* % %) (intToDigits2 n))))))
(defn newHappy [n] (letfn
[(hNum [x] (reduce + (map #(* % %) (intToDigits x))))
(intToDigits [x] (map #(Integer/parseInt (str %)) (seq (str x))))]
(loop
[prev #{}
initial (hNum n)]
(cond
(= initial 1) true
(contains? prev initial) false
:else (recur (conj prev initial) (hNum initial))))))
;(println (HappyNumbers 3))
(println (newHappy 3))