Created
April 11, 2014 06:29
-
-
Save matthiasak/10444135 to your computer and use it in GitHub Desktop.
Combinatorics with constraints
This file contains 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
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