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;
if (result === Infinity) return Infinity;
}
return result;
}
function calculatePermCombi() {
var n = parseInt(document.getElementById("totalN").value);
var r = parseInt(document.getElementById("sampleR").value);
var errorDiv = document.getElementById("errorDisplay");
var pBox = document.getElementById("permutationResult");
var cBox = document.getElementById("combinationResult");
errorDiv.style.display = "none";
pBox.style.display = "none";
cBox.style.display = "none";
if (isNaN(n) || isNaN(r) || n < 0 || r n) {
errorDiv.style.display = "block";
return;
}
// Calculation Logic
// Permutation P(n,r) = n! / (n-r)!
// Combination C(n,r) = n! / (r! * (n-r)!)
var nFact = factorial(n);
var rFact = factorial(r);
var nMinusRFact = factorial(n – r);
var permutations = nFact / nMinusRFact;
var combinations = nFact / (rFact * nMinusRFact);
document.getElementById("pVal").innerText = isFinite(permutations) ? permutations.toLocaleString() : "Number too large";
document.getElementById("cVal").innerText = isFinite(combinations) ? combinations.toLocaleString() : "Number too large";
pBox.style.display = "block";
cBox.style.display = "block";
}
Understanding Permutations and Combinations
In the world of statistics and probability, we often need to determine the number of ways we can select items from a group. Whether you are choosing a lottery number, organizing a seating chart, or selecting a project team, understanding the difference between a permutation and a combination is essential.
What is a Permutation (nPr)?
A permutation is an arrangement of items where the order matters. For example, the combination to a safe is actually a permutation because the sequence "1-2-3" is different from "3-2-1".
The Permutation Formula:
P(n, r) = n! / (n – r)!
n: Total number of objects in the set.
r: Number of objects selected.
!: Factorial (e.g., 4! = 4 × 3 × 2 × 1 = 24).
What is a Combination (nCr)?
A combination is a selection of items where the order does NOT matter. If you are picking three fruits for a smoothie, "Apple, Banana, Orange" is the same as "Orange, Apple, Banana".
The Combination Formula:
C(n, r) = n! / (r! * (n – r)!)
Practical Examples
Example 1: The Race (Permutation)
There are 8 runners in a race. How many ways can we award Gold, Silver, and Bronze medals?
Here, n = 8 and r = 3. Because the rank (order) matters, we use Permutations.
Calculation: 8! / (8-3)! = 8! / 5! = 336 ways.
Example 2: The Committee (Combination)
There are 10 employees, and you need to pick a committee of 3. How many ways can you choose them?
Here, n = 10 and r = 3. Since the order of people in the committee doesn't change the committee itself, we use Combinations.
Calculation: 10! / (3! * 7!) = 120 ways.
Frequently Asked Questions
Can r be larger than n? No. You cannot select more items than are available in the total set. Our calculator will show an error if r is greater than n.
What is 0! (zero factorial)? In mathematics, 0! is defined as 1. This ensures that the formulas for permutations and combinations work correctly even when you select all items from a set (n=r).
Why is the combination result always smaller or equal to the permutation result? Because in combinations, we treat different orders of the same items as a single unique selection, effectively dividing the total permutations by r!.