programming-examples/js/Math/Calculate the combination of n and r.js

26 lines
521 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function product_Range(a,b) {
var prd = a,i = a;
while (i++< b) {
prd*=i;
}
return prd;
}
function combinations(n, r)
{
if (n==r)
{
return 1;
}
else
{
r=(r < n-r) ? n-r : r;
return product_Range(r+1, n)/product_Range(1,n-r);
}
}
console.log(combinations(6, 2));
console.log(combinations(5, 3));