Skip to content

Instantly share code, notes, and snippets.

@namenu
Last active July 29, 2018 06:54
Show Gist options
  • Save namenu/f1e8d0a088a327a48a7e902292f89e59 to your computer and use it in GitHub Desktop.
Save namenu/f1e8d0a088a327a48a7e902292f89e59 to your computer and use it in GitHub Desktop.
Recamán Sequence
(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