Skip to content

Instantly share code, notes, and snippets.

@arthuredelstein
Created February 17, 2013 04:08
Show Gist options
  • Save arthuredelstein/4970091 to your computer and use it in GitHub Desktop.
Save arthuredelstein/4970091 to your computer and use it in GitHub Desktop.
Exploring the Hailstone Sequence in Clojure. See https://en.wikipedia.org/wiki/HOTPO
(defn hotpo
"One iteration of the hailstone sequence."
[n]
(if (even? n)
(/ n 2)
(inc (* 3 n))))
(defn hotpot [x]
"Finds a hailstone sequence starting with integer x."
(take-while #(> % 1)
(iterate hotpo x)))
(defn hotpotato
"Returns the length of a hailstone sequence starting with integer x."
[x]
(count (hotpot x)))
(defn hottest-potato
"Finds the longest hailstone sequence starting with an integer less than y."
[y]
(apply max-key hotpotato (range y)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment