Last active
July 29, 2018 06:54
-
-
Save namenu/f1e8d0a088a327a48a7e902292f89e59 to your computer and use it in GitHub Desktop.
Recamán Sequence
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 recaman | |
([] | |
(recaman 1 1 #{1})) | |
([n prev visited] | |
(lazy-seq | |
(cons prev | |
(let [n (inc n) | |
next (let [lookup (- prev n)] | |
(if (and (pos? lookup) (nil? (visited lookup))) | |
lookup | |
(+ prev n)))] | |
(recaman n next (conj visited next))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment