Combinations and permutations
“How many ways” has four answers, and they are not close to each other. All four, computed exactly, without ever building a factorial. Nothing you type here leaves your browser — it is worked out on your own device, so there is nothing to send and nothing to log.
“How many ways” has four answers
And which one you want depends on two questions that almost nobody asks out loud. This page answers all four rather than guessing.
| Order matters, no repeats | Permutations. Gold, silver and bronze from a field of runners. |
|---|---|
| Order matters, repeats allowed | nr. A four-digit PIN: 10 × 10 × 10 × 10. |
| Order does not matter, no repeats | Combinations. Six lottery numbers. Which order they come out in is nobody's business. |
| Order does not matter, repeats allowed | Three scoops from eight flavours, two of the same allowed. |
For n = 10 and r = 3 those four answers are 120, 720, 220 and 1,000. They are not close to each other, and picking the wrong one is not a rounding error.
n! overflows long before the answer does
Every textbook writes combinations as n! ÷ (r!(n−r)!) and permutations as n! ÷ (n−r)!. Implement those literally and they break for inputs a pocket calculator handles.
In a browser, 170! is 7.26 × 10306 and 171! is Infinity. So:
| C(1000, 3) | The answer is 166,167,000. Via factorials it is Infinity ÷ Infinity, which is NaN. |
|---|---|
| P(1000, 3) | The answer is 997,002,000. Via factorials, NaN. |
“How many ways can three runners finish first, second and third in a field of a thousand?” is a question a child can ask and a great many calculators cannot answer.
The fix: never build the big number
Multiply and divide alternately instead:
C(n,r) = (n−r+1)/1 ×
(n−r+2)/2 × … × n/r
Every partial product is itself a binomial coefficient, so nothing ever grows bigger than the answer. And because C(n,r) = C(n, n−r), taking the smaller of r and n−r halves the work — C(1000, 997) is three multiplications, not 997.
Past 253 an ordinary number stops counting
A browser's ordinary numbers hold every whole number exactly up to 9,007,199,254,740,991, and after that they start skipping. C(57, 28) is the first combination that lands past it, and an ordinary calculation gives 15,033,633,249,770,518 where the true answer is 15,033,633,249,770,520.
So this page computes every count twice: once as an ordinary number and once in arbitrary-precision integers, and shows the exact figure whenever the two disagree. The lottery odds are exact. The number of ways to deal a bridge hand is exact, all twenty-nine digits of it.
The numbers people actually want
| UK Lotto | 6 from 59 = 45,057,474 ways |
|---|---|
| A five-card poker hand | 5 from 52 = 2,598,960 |
| A bridge hand | 13 from 52 = 635,013,559,600 |
| Shuffling a deck | 52! — about 8 × 1067, which is more than the number of atoms in the galaxy, and why every shuffle you have ever seen was almost certainly new |
What it will not do
- It will not work out probabilities. How many ways is not the same question as how likely, and the second needs to know whether the ways are equally likely.
- It will not handle multisets with specified repeats — “how many arrangements of MISSISSIPPI” is a different formula.
- It will not go past what your browser can hold in memory. The exact figure for enormous n is a very long number and it is computed digit by digit.