From 7ae85dcfe371b206ce58eb789268601b92f5ce3d Mon Sep 17 00:00:00 2001 From: Alexander Eckart Date: Mon, 17 Dec 2012 15:48:55 +0100 Subject: [PATCH] added nCk calculation, don't know the limit... --- VocaluxeLib/Menu/CHelper.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/VocaluxeLib/Menu/CHelper.cs b/VocaluxeLib/Menu/CHelper.cs index 815e941f..ad430fba 100644 --- a/VocaluxeLib/Menu/CHelper.cs +++ b/VocaluxeLib/Menu/CHelper.cs @@ -11,6 +11,31 @@ namespace Vocaluxe.Menu { public class CHelper { + public static int nCk(int n, int k) + { + if (k > n) + return 0; + + if (k == 0 || k == n) + return 1; + + if (k < 0 || n <= 0) + return 0; //is not defined + + if (k * 2 > n) + k = n - k; + + long result = n; + long nl = n; + long nk = k; + + for (long i = 1; i <= k - 1; i++) + { + result = result * (n - i) / (i + 1); + } + return (int)result; + } + /// /// Concat strings into one string with ", " as separator. ///