Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
Last active March 18, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sw-samuraj/5d2a0b922e40dc16f75035a789db8e97 to your computer and use it in GitHub Desktop.
Save sw-samuraj/5d2a0b922e40dc16f75035a789db8e97 to your computer and use it in GitHub Desktop.
Counts a Catalan number through a binomial coefficient.
(defn factorial [n]
"Counts a factorial."
(reduce *' (range 1 (inc n))))
(defn binomial [n k]
"Counts a binomial coefficient."
(/ (factorial n)
(*' (factorial k)
(factorial (- n k)))))
(defn catalan [n]
"Counts a Catalan number."
(* (/ 1 (inc n))
(binomial (* 2 n) n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment