This article explains the advanced function features in shell scripting—scoping, recursion, return values, and error handling—that go beyond basic function definitions. You’ll learn how to write robust, reusable functions that handle real-world complexity without common pitfalls.
Formula
The calculator above computes the number of combinations (nCr) using the factorial formula:
C(n, r) = n! / (r! × (n − r)!)
Where:
- n = total number of items (integer ≥ 0)
- r = number of items to choose (integer, 0 ≤ r ≤ n)
- ! = factorial (e.g., 5! = 5×4×3×2×1 = 120)
The result is the number of ways to select r items from a set of n items without regard to order.
Worked Example
Using the default values (n=5, r=2):
- Compute 5! = 5×4×3×2×1 = 120
- Compute 2! = 2×1 = 2
- Compute (5−2)! = 3! = 3×2×1 = 6
- Plug into formula: 120 / (2 × 6) = 120 / 12 = 10
The calculator returns 10, which is correct: choosing 2 items from 5 gives 10 possible pairs.
When to Use Combinations
Combinations appear in probability, statistics, and everyday counting problems. Use them when you need to know how many groups can be formed from a larger set, and the order inside the group doesn’t matter. For example:
- How many 3-topping pizzas can you make from 10 toppings?
- How many 5-card hands are possible from a 52-card deck?
- How many ways can a committee of 4 be chosen from 12 people?
If order matters (e.g., first, second, third place), use permutations instead (nPr = n! / (n−r)!).
How to Read the Result
The result is always a non-negative integer. A larger n or r closer to n/2 gives a bigger number. For example, C(10,2) = 45, but C(10,5) = 252. The maximum value for a given n occurs at r = n/2 (if n is even).
If r = 0 or r = n, the result is 1 (only one way to choose nothing or everything). If r > n, the formula would produce a fractional value, but our calculator blocks that with a validation error.
Common Mistakes
- Confusing combinations with permutations: Combinations ignore order; permutations count order. For 5 items taken 2 at a time, combinations = 10, permutations = 20.
- Using decimals: n and r must be integers. The factorial function is only defined for non-negative integers.
- Forgetting that 0! = 1: This is a mathematical convention, and it’s essential for the formula to work when r = 0 or r = n.
- Rounding errors: The calculator returns an integer, but if you compute manually, avoid rounding intermediate factorial values (they grow fast).
Limits of the Formula
The factorial function grows extremely quickly. For n above 170, the result exceeds the range of standard floating-point numbers (about 10^308), so the calculator may show an error or scientific notation. In practice, most real-world problems use n < 100, so this is rarely an issue. Also, the formula assumes you are choosing without replacement—once an item is selected, it can't be selected again. If you need to allow repeats (e.g., rolling dice), use combinations with repetition: C(n+r−1, r).
Alternatives
If you need to calculate permutations, use nPr = n! / (n−r)!. If you need to count arrangements of all items, use n! directly. For probabilities, you often divide the number of favorable combinations by the total number of combinations. Many programming languages have built-in functions (e.g., Python’s math.comb) that handle large numbers more efficiently than the factorial method.
Frequently Asked Questions
What is the difference between combinations and permutations?
Combinations count selections where order doesn’t matter (e.g., choosing 2 fruits from a basket). Permutations count arrangements where order does matter (e.g., assigning 2 prizes to 2 people). For n=5, r=2, combinations = 10, permutations = 20.
Why is 0! equal to 1?
It’s a mathematical convention that makes formulas work consistently. There is exactly one way to arrange zero objects, and it also makes the combination formula give C(n,0) = 1, which matches intuition (choosing nothing is one option).
Can I use this for lottery odds?
Yes, but be careful: lottery odds typically use combinations without replacement. For a 6/49 lottery, the total combinations are C(49,6) = 13,983,816. Your chance of winning with one ticket is 1 in that number.
What if r is larger than n?
Mathematically, C(n,r) is defined as 0 when r > n, because you can’t choose more items than you have. Our calculator blocks this input to avoid confusion.
How do I calculate combinations with repetition?
Use the formula C(n+r−1, r). For example, choosing 3 scoops of ice cream from 5 flavors (with repeats allowed) gives C(5+3−1, 3) = C(7,3) = 35.
Related
Formula used
in n | Number of items | - | 5 | 1 | 100 in r | Items chosen | - | 2 | 0 | 100 out result | Combinations (nCr) | - | fact(n) / (fact(r) * fact(n - r)) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Channel TLD Websites: What They Are & How to Get One5.00 min/km
- Django Class Based Views CRUD REST API
- Tmux Terminal Upgrade: Is It Worth It?
- Most Secure OS for Privacy: 3 Picks by Device
- Ryobi One+ Battery System: Is the Switch Worth It?
- Refurbished Tech Buying Checklist