r/ProgrammingPrompts • • Mar 07 '14
Collatz Conjecture!
Create a program which performs and prints to the screen the Collatz Conjecture of any number input by the user.
(the Collatz Conjecture is a conjecture by which a number is taken and if it is even, the number is halved, and if it is odd, the number is tripled and added to by one.)
21
Upvotes
View all comments
1
u/bliow Mar 09 '14 edited Mar 09 '14
Here's a Clojure version. Hope it encourages someone to take up this awesome language.
Could've written this much more concisely but I wanted to structure this program sensibly and show why Clojure's cool, not play code golf and construct incomprehensible one-liners.
I kind of regret not taking a hint from iminurnamez' approach, signaling the end of the sequence with
nil+ writing(defn collatz-sequence [n] (take-while (complement nil?) (iterate collatz n))). But I like the idea of acollatzthat matches the actual sequence, including the repeated 1, 4, 2, 1, 4, 2, 1, ...