]> git.ozlabs.org Git - ccan/blobdiff - junkcode/tterribe@email.unc.edu-nmbrthry/choose.c
New junkcode from Tim.
[ccan] / junkcode / tterribe@email.unc.edu-nmbrthry / choose.c
diff --git a/junkcode/tterribe@email.unc.edu-nmbrthry/choose.c b/junkcode/tterribe@email.unc.edu-nmbrthry/choose.c
new file mode 100644 (file)
index 0000000..54047bf
--- /dev/null
@@ -0,0 +1,25 @@
+#include "choose.h"
+#include "gcd.h"
+
+/*Computes the number of combinations of _n items, taken _m at a time without
+   overflow.
+  _n: The total number of items.
+  _m: The number taken at a time.
+  Return: The number of combinations of _n items taken _m at a time.*/
+unsigned choose(int _n,int _m){
+  unsigned ret;
+  int      i;
+  ret=1;
+  for(i=1;i<=_m;_n--,i++){
+    int nmi;
+    nmi=_n%i;
+    if(nmi==0)ret*=_n/i;
+    else if(ret%i==0)ret=(ret/i)*_n;
+    else{
+      int d;
+      d=gcd(i,nmi);
+      ret=(ret/(i/d))*(_n/d);
+    }
+  }
+  return ret;
+}