Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
17
Task/Hailstone-sequence/Clojure/hailstone-sequence.clj
Normal file
17
Task/Hailstone-sequence/Clojure/hailstone-sequence.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn hailstone-seq [n]
|
||||
{:pre [(pos? n)]}
|
||||
(lazy-seq
|
||||
(cond (= n 1) '(1)
|
||||
(even? n) (cons n (hailstone-seq (/ n 2)))
|
||||
:else (cons n (hailstone-seq (+ (* n 3) 1))))))
|
||||
|
||||
(let [hseq (hailstone-seq 27)]
|
||||
(-> hseq count (= 112) assert)
|
||||
(->> hseq (take 4) (= [27 82 41 124]) assert)
|
||||
(->> hseq (drop 108) (= [8 4 2 1]) assert))
|
||||
|
||||
(let [{max-i :num, max-len :len}
|
||||
(reduce #(max-key :len %1 %2)
|
||||
(for [i (range 1 100000)]
|
||||
{:num i, :len (count (hailstone-seq i))}))]
|
||||
(println "Maximum length" max-len "was found for hailstone(" max-i ")."))
|
||||
Loading…
Add table
Add a link
Reference in a new issue