Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Hamming-numbers/Clojure/hamming-numbers-1.clj
Normal file
17
Task/Hamming-numbers/Clojure/hamming-numbers-1.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn smerge [xs ys]
|
||||
(lazy-seq
|
||||
(let [x (first xs),
|
||||
y (first ys),
|
||||
[z xs* ys*]
|
||||
(cond
|
||||
(< x y) [x (rest xs) ys]
|
||||
(> x y) [y xs (rest ys)]
|
||||
:else [x (rest xs) (rest ys)])]
|
||||
(cons z (smerge xs* ys*)))))
|
||||
|
||||
(def hamming
|
||||
(lazy-seq
|
||||
(->> (map #(*' 5 %) hamming)
|
||||
(smerge (map #(*' 3 %) hamming))
|
||||
(smerge (map #(*' 2 %) hamming))
|
||||
(cons 1))))
|
||||
13
Task/Hamming-numbers/Clojure/hamming-numbers-2.clj
Normal file
13
Task/Hamming-numbers/Clojure/hamming-numbers-2.clj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defn hamming
|
||||
"Computes the unbounded sequence of Hamming 235 numbers."
|
||||
[]
|
||||
(letfn [(merge [xs ys]
|
||||
(if (nil? xs) ys
|
||||
(let [xv (first xs), yv (first ys)]
|
||||
(if (< xv yv) (cons xv (lazy-seq (merge (next xs) ys)))
|
||||
(cons yv (lazy-seq (merge xs (next ys)))))))),
|
||||
(smult [m s] ;; equiv to map (* m) s -- faster
|
||||
(cons (*' m (first s)) (lazy-seq (smult m (next s))))),
|
||||
(u [s n] (let [r (atom nil)]
|
||||
(reset! r (merge s (smult n (cons 1 (lazy-seq @r)))))))]
|
||||
(cons 1 (lazy-seq (reduce u nil (list 5 3 2))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue