A combination is a selection of items from a larger set where the order of selection does not matter. In mathematics, this is often referred to as "n choose r." For example, if you are picking three people for a committee from a group of ten, it doesn't matter if Person A is picked first or third; the committee remains the same.
The nCr Formula
The mathematical formula to calculate combinations is:
C(n, r) = n! / (r! * (n – r)!)
Where:
n is the total number of items in the set.
r is the number of items you are choosing.
! denotes a factorial (the product of all positive integers up to that number).
Example Calculation
Suppose you have a deck of 52 cards and you want to know how many possible 5-card hands can be dealt. Here, n = 52 and r = 5.
Calculate 52! (This is a massive number).
Calculate 5! (120).
Calculate (52 – 5)! which is 47!.
Apply the formula: 52! / (5! * 47!).
The result is 2,598,960 possible combinations.
Combinations vs. Permutations
The primary difference is order. In permutations, the order matters (like a door lock code 1-2-3). In combinations, the order does not matter (like choosing three fruits for a smoothie). Because order doesn't matter, there are always fewer combinations than permutations for the same set of numbers.
function factorial(num) {
if (num < 0) return -1;
if (num === 0 || num === 1) return 1;
var result = 1;
for (var i = 2; i <= num; i++) {
result *= i;
}
return result;
}
function calculateCombinations() {
var n = parseInt(document.getElementById('total_n').value);
var r = parseInt(document.getElementById('choose_r').value);
var resultBox = document.getElementById('combination_result_box');
var outputText = document.getElementById('calc_output_text');
var formulaText = document.getElementById('calc_formula_text');
if (isNaN(n) || isNaN(r)) {
alert("Please enter valid numbers for both n and r.");
return;
}
if (n < 0 || r n) {
alert("The number of items chosen (r) cannot be greater than the total items (n).");
return;
}
// Since large factorials cause overflow, use a more optimized calculation for nCr
// C(n, r) = n! / (r! * (n-r)!)
var combinations = 1;
// Optimization: C(n, r) is the same as C(n, n-r)
if (r > n / 2) r = n – r;
for (var i = 1; i <= r; i++) {
combinations = combinations * (n – i + 1) / i;
}
// Round to handle floating point precision issues
var finalResult = Math.round(combinations);
outputText.innerHTML = "Total Combinations: " + finalResult.toLocaleString();
formulaText.innerHTML = "Formula Applied: " + n + "! / (" + r + "! * (" + n + " – " + r + ")!)";
resultBox.style.display = 'block';
}