Skip to content

Instantly share code, notes, and snippets.

@Lugoues
Last active July 13, 2018 08:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lugoues/1b23c5e93e3eff44729e to your computer and use it in GitHub Desktop.
Save Lugoues/1b23c5e93e3eff44729e to your computer and use it in GitHub Desktop.
Clojure: Stern-Brocot Sequence
;; See: http://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree
(def stern-brocot-sequence
(map (fn [nums]
(/ (first nums) (second nums)))
(iterate
(fn [nums]
(let [a (first nums)
b (second nums)
r (rest (rest nums))]
(vec (flatten (conj [] b r (+ a b) b)))))
[1 1])))
(comment
(pprint (take 40 stern-brocot-sequence))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment