Is there a math nCr function in Python?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Yes. Python 3.8+ has math.comb(n, r) which computes the binomial coefficient nCr (the number of ways to choose r items from n items without order). It returns an exact integer, handles edge cases (r > n returns 0, r = 0 returns 1), and is implemented in C for speed. For older Python versions, use math.factorial(n) // (math.factorial(r) * math.factorial(n - r)) or scipy.special.comb. For permutations (nPr), use math.perm(n, r) (also Python 3.8+).
math.comb (Python 3.8+)
math.perm — Permutations (Python 3.8+)
For Older Python Versions (Pre-3.8)
Using scipy.special.comb
Practical Applications
Generating Actual Combinations
Common Pitfalls
- Using
math.combon Python < 3.8:math.combwas added in Python 3.8. On older versions, you getAttributeError: module 'math' has no attribute 'comb'. Use the factorial formula or installscipyas a fallback. Check your version withsys.version_info >= (3, 8). - Confusing
comb(combinations) withperm(permutations):math.comb(5, 2)= 10 (unordered),math.perm(5, 2)= 20 (ordered). If order matters (arrangements), useperm. If order does not matter (selections), usecomb. - Integer overflow in manual implementations: The factorial formula
n! / (r! * (n-r)!)computes very large intermediate values. Forcomb(1000, 500),1000!has thousands of digits. Python handles big integers natively, but iterative multiplication-division (likecomb_efficientabove) avoids huge intermediates and runs faster. scipy.special.combreturning float by default: Withoutexact=True,scipy.special.combreturns a float, which loses precision for large values.comb(100, 50)as a float has rounding errors. Always passexact=Truewhen you need an exact integer result.- Negative inputs silently returning wrong results in custom functions:
math.combraisesValueErrorfor negative inputs, but a naive factorial-based implementation may not check. Always validate thatn >= 0andr >= 0in custom implementations to match the mathematical definition.
Summary
- Use
math.comb(n, r)for combinations (Python 3.8+) — exact integer, C-speed - Use
math.perm(n, r)for permutations (Python 3.8+) - For Python < 3.8, use iterative multiplication-division to avoid large intermediate factorials
- Use
itertools.combinationsto generate actual combinations,math.combto count them - For array/batch operations, use
scipy.special.combwithexact=Truefor precision
Related reading
- Is there a pseudo-random number generator simple enough to do in your head?
- Is there a sequence of swaps that would generate all possible permutations?
- Is there a simple algorithm that can determine if X is prime?
- Is there a way to find sum of digits of 100?
- Is there a not equal operator in Python?
- Is there a NumPy function to return the first index of something in an array?
- Is there an algorithm for solving such projection reconstruction geometric problem?
- Is there an algorithm that can divide a number into three parts and have their totals match the original number?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.