From ee20db43900ace53bca871b48042582fef426191 Mon Sep 17 00:00:00 2001 From: Ted V Date: Fri, 27 May 2005 23:04:42 +0000 Subject: [PATCH] Added (N choose K) function. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3580 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/media/util/MathUtil.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/java/com/threerings/media/util/MathUtil.java b/src/java/com/threerings/media/util/MathUtil.java index 55f9f8a31..298313077 100644 --- a/src/java/com/threerings/media/util/MathUtil.java +++ b/src/java/com/threerings/media/util/MathUtil.java @@ -144,4 +144,19 @@ public class MathUtil // the standard deviation is the square root of the variance return new float[] { mean, variance, (float)Math.sqrt(variance) }; } + + /** + * Computes (N choose K), the number of ways to select K different + * elements from a set of size N. + */ + public static int choose (int n, int k) + { + // Base case: One way to select or not select the whole set + if (k <= 0 || k >= n) { + return 1; + } + + // Recurse using pascal's triangle + return (choose(n-1, k-1) + choose(n-1, k)); + } }