Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defn compute-collatz [num steps]
(cond
(= num 1) steps
(even? num) (compute-collatz (/ num 2) (inc steps))
(odd? num) (compute-collatz (+ 1 (* num 3)) (inc steps))))
(defn collatz [num]
(if
(pos? num) (compute-collatz num 0)
(throw (AssertionError. "Positive integer required."))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment