Created
July 15, 2011 08:37
-
-
Save jColeChanged/1084331 to your computer and use it in GitHub Desktop.
Today I programmed
This file contains hidden or 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
(use 'clojure.contrib.math) | |
(def factorial | |
(fn [n] | |
(loop [cnt n acc 1] | |
(if (zero? cnt) | |
acc | |
(recur (dec cnt) (* acc cnt)))))) | |
(defn perm | |
"Just the formula for calculating the number of perms given n and k." | |
[n k] | |
(/ (factorial n) (factorial (- n k)))) | |
(defn choose | |
"Just the formula for calculating the number n choose k." | |
[n k] | |
(/ (perm n k) (factorial k))) | |
(defn binomial-expand | |
"The binomial theorem gives a way of expanding binomials raised to the k | |
power. This method is far simpler than multipling things out. It is also | |
something I spent a little time mulling over. I'm just writing this out so | |
that I can get a better feel for it." | |
[x y n] | |
(apply + (for [k (range 0 (inc n))] | |
(* (choose n k) (expt x (- n k)) (expt y k))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment