Combinations & Permutations
Calculate combinations and permutations for counting problems, with a clear test for which one your problem actually needs.
| Way of counting | Order | Repeats | Ways |
|---|---|---|---|
| Combinations — C(n,r) | ignored | no | — |
| Multiset — C(n+r−1,r) | ignored | yes | — |
| Permutations — P(n,r) | matters | no | — |
| Tuples — nr | matters | yes | — |
n! (factorial of n): —
The outer two never move; the middle two swap four times
Across every pair with r no larger than n up to 60, plain combinations are always the smallest of the four and nr is always the largest. Neither ever changes place. The two in the middle do, and hardly ever: the multiset count beats the permutation count in exactly 4 of those pairs, and every one of them has r equal to n — the 2, 3, 4, 5. The last is 5 choosing 5, where 126 edges out 120 by six, and from 6 upward the falling factorial pulls away for good. There is also exactly one non-trivial tie in the range: 3 choosing 2, where both come to 6. (At r = 1 all four are just n, which ties everywhere for a boring reason.)
The textbook formula gets under a quarter of its answers right
Every combinatorics course writes C(n,r) as n! over r!(n−r)!. Compute it that way in ordinary floating point, across every pair with n from 0 to 200:
| Outcome | Pairs | Share |
|---|---|---|
| Exact | 4,726 | 23.3% |
| Wrong, with no warning | 9,980 | 49.2% |
| Overflowed to infinity | 5,595 | 27.6% |
The middle group is the interesting one. C(55,24) is the first to go wrong and it comes out as 2488589544741301 when the answer is 2488589544741300 — one too many, with nothing about the number to suggest it. Wrong answers outnumber overflows by 1.8 to one.
The overflows are at least loud. They start at C(171,0), whose answer is 1 — the formula computes 171! before it does anything else, and 171! is larger than a double can hold.
The order of operations is the whole problem
C(200,100) is about 9 × 10⁵⁸. Large, and nothing an arbitrary-precision integer cannot hold. The trouble is that the textbook formula asks for 200! along the way, and 200! is three hundred digits larger than the answer it is being used to compute.
| Answer | Falling product | Via n factorial | |
|---|---|---|---|
| C(50,25) | 15 digits | 40 | 65 |
| C(100,50) | 30 digits | 94 | 158 |
| C(200,100) | 59 digits | 217 | 375 |
| C(1000,500) | 300 digits | 1,434 | 2,568 |
Building an enormous intermediate on the way to a modest result is the mistake, not the size of the result. This page multiplies the falling product instead — n × (n−1) × … × (n−r+1) — and divides by r!. That is still bigger than the answer, by a factor of r!, but it is around half the digits of the factorial route.
The division is whole-number division, which discards any remainder — alarming if a remainder were possible. It is not: r consecutive integers always contain a multiple of r, of r−1, and so on, so their product is always divisible by r!. Checked rather than assumed, across all 1,891 pairs with n up to 60: 0 had anything left over.
Where a double-precision factorial actually stops
Later than the safe-integer limit suggests. Whole numbers stay exact up to 9,007,199,254,740,991, which 21! passes comfortably — and 21! and 22! are still exact anyway, because a factorial gathers factors of two and the low bits it would lose are zeros. A double carries 53 significant bits, so a value of B bits needs B − 53 of its low bits to be zero.
| Bits | Zeros needed | Zeros it has | Spare | |
|---|---|---|---|---|
| 20! | 62 | 9 | 18 | +9 |
| 21! | 66 | 13 | 18 | +5 |
| 22! | 70 | 17 | 19 | +2 |
| 23! | 75 | 22 | 19 | -3 |
The margin shrinks and runs out at exactly 23, which is why 23! is the first one that comes back wrong. This page uses arbitrary-precision integers and has no such limit; the scientific calculator elsewhere on this site uses doubles and does.
Which raises a display problem rather than an arithmetic one. Permutations of 5000 from 5000 is a 16,326-digit number, and printed in full with thousands separators it is 21,767 characters. All three results are now cut at sixty digits with the total appended, so a large answer says how large it is instead of filling the page.
How to use
- Enter the total number of items and how many you are choosing.
- Decide whether order matters.
- Read the result and the formula used.
- Check whether repetition is allowed in your problem.
Frequently asked questions
How do I tell which one I need?
Ask whether rearranging the chosen items gives a different outcome. A committee of three people is the same committee whichever order you named them, so that is a combination. First, second and third place in a race are different results, so that is a permutation. Permutations always give the larger number.
What are the formulas?
Permutations of r items from n are n factorial divided by the factorial of n minus r. Combinations divide that again by r factorial, which removes the count of orderings within each selection. The relationship between them is exactly that factor of r factorial.
What is a factorial?
The product of every whole number from 1 up to that number, so 5 factorial is 120. It counts the ways of arranging that many distinct items. Factorials grow astonishingly fast — 20 factorial already exceeds two quintillion — which is why counting problems become intractable so quickly.
Why is 0 factorial equal to 1?
Because there is exactly one way to arrange nothing: the empty arrangement. It is a definition, but not an arbitrary one — it is the value that makes the combination formula work correctly when choosing all or none of the items.
What if repetition is allowed?
The formulas change. Choosing r from n with repetition and order mattering is simply n to the power r. With repetition and order not mattering, it is a combination of n plus r minus 1 choose r — the stars-and-bars result, which is considerably less obvious.
How does this relate to lottery odds?
Directly. Picking 6 numbers from 49 without order is a combination, giving just under 14 million possibilities and therefore those odds against a single ticket. Seeing the arithmetic tends to be more informative than any amount of advice about the lottery.
🔒 This tool runs entirely in your browser. Nothing you enter is uploaded, logged, or stored.