Skip to content

Instantly share code, notes, and snippets.

@matthiasak
Created April 11, 2014 06:29
Show Gist options
  • Save matthiasak/10444135 to your computer and use it in GitHub Desktop.
Save matthiasak/10444135 to your computer and use it in GitHub Desktop.
Combinatorics with constraints
function factorial(n){
return (n < 2) ? 1 : n * factorial(n-1);
}
function C(n,k){
return factorial(n) / (factorial(k) * factorial(n-k));
}
function maxPossibleConfigurations(a, b, c, n){
var args= [].slice.call(arguments);
if(args.length < 4) throw new Error("invalid arguments ["+args.join(", ")+"]");
if(a+b+c<n) return 0;
return C(2+n, 2) - C(2+n-a, 2) - C(2+n-b, 2) - C(2+n-c, 2) + 2*C(2+n-a-b, 2) + 2*C(2+n-b-c, 2) + 2*C(2+n-a-c, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment