Created
February 17, 2013 04:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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