June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
28
Task/Executable-library/Clojure/executable-library-1.clj
Normal file
28
Task/Executable-library/Clojure/executable-library-1.clj
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(ns rosetta-code.hailstone-sequence)
|
||||
|
||||
(defn next-in-hailstone
|
||||
"Returns the next number in the Hailstone sequence that starts with x.
|
||||
If x is less than 2, returns nil."
|
||||
[x]
|
||||
(when (> x 1)
|
||||
(if (even? x)
|
||||
(/ x 2)
|
||||
(inc (* 3 x)))))
|
||||
|
||||
(defn hailstone-seq
|
||||
"Returns a lazy Hailstone sequence starting with the number x."
|
||||
[x]
|
||||
(take-while some?
|
||||
(iterate next-in-hailstone x)))
|
||||
|
||||
(defn -main [& args]
|
||||
(let [h27 (hailstone-seq 27)]
|
||||
(printf "The Hailstone sequence starting at 27 contains %s elements:\n%s ... %s.\n"
|
||||
(count h27)
|
||||
(vec (take 4 h27))
|
||||
(vec (take-last 4 h27)))
|
||||
(let [[number length] (apply max-key second
|
||||
(map (fn [x] [x (count (hailstone-seq x))])
|
||||
(range 100000)))]
|
||||
(printf "The number %s has the longest Hailstone sequence under 100000, of length %s.\n"
|
||||
number length))))
|
||||
13
Task/Executable-library/Clojure/executable-library-2.clj
Normal file
13
Task/Executable-library/Clojure/executable-library-2.clj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(ns rosetta-code.frequent-hailstone-lengths
|
||||
(:require [rosetta-code.hailstone-sequence
|
||||
:refer [hailstone-seq]]))
|
||||
|
||||
(defn -main [& args]
|
||||
(let [frequencies (apply merge-with +
|
||||
(for [x (range 1 100000)]
|
||||
{(count (hailstone-seq x)) 1}))
|
||||
[most-frequent-length frequency]
|
||||
(apply max-key val (seq frequencies))]
|
||||
(printf (str "The most frequent Hailstone sequence length for numbers under 100000 is %s,"
|
||||
" with a frequency of %s.\n")
|
||||
most-frequent-length frequency)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue