Skip to main content

How to Calculate Combinations (nCr) in Batch Script

A Combination counts the number of ways to choose $r$ items from a set of $n$ items, where the Order Does Not Matter. The formula is: $C(n, r) = n! / (r! \times (n - r)!)$. For example, $C(5, 2) = 10$, meaning there are 10 ways to pick 2 items from a set of 5. Combinations are used in lottery probability, team selection, and subset analysis.

In this guide, we will demonstrate how to calculate combinations using an optimized multiplicative approach.

The Strategy: The Iterative Formula​

To avoid the massive numbers produced by factorials, we use the iterative formula: C(n, r) = [n Ɨ (nāˆ’1) Ɨ … Ɨ (nāˆ’r+1)] / r!

By interleaving multiplication and division, we keep intermediate values as small as possible.

Implementation Script​

@echo off
setlocal enabledelayedexpansion

set /p "n=Enter n (total items): "
set /p "r=Enter r (items to choose): "

:: Validate inputs
if !r! GTR !n! (
echo [ERROR] r cannot be greater than n.
pause
exit /b
)

:: Optimization: C(n,r) == C(n, n-r). Use the smaller r.
set /a "alt=n - r"
if !alt! LSS !r! set /a "r=alt"

:: Calculate using iterative formula
set "result=1"
for /L %%i in (1,1,!r!) do (
set /a "numerator=n - %%i + 1"
set /a "result=!result! * numerator / %%i"
)

echo.
echo ==========================================
echo C(!n!, !r!) = !result!
echo ==========================================
pause
Intermediate Overflow

The expression set /a "result=!result! * numerator / %%i" multiplies before dividing in a single set /a statement. Batch evaluates this left-to-right, so the intermediate product result * numerator must fit within a signed 32-bit integer (max 2,147,483,647). For large values of n, this intermediate product can overflow before the division brings it back down.

Why Calculate Combinations?​

  1. Lottery Probability: Calculating the odds of winning a lottery (e.g., choosing 6 numbers from 49) is $C(49, 6) = 13,983,816$.
  2. Team Formation: Calculating how many unique 3-person teams can be formed from a group of 10 volunteers.
  3. Subset Analysis: Determining how many unique groups of servers can be selected for a test deployment from a larger pool.

Important Limitations​

32-Bit Overflow

While the iterative formula helps, large combinations (like $C(30, 15) = 155,117,520$) can still cause intermediate overflow during the multiplication step. Values of n beyond about 30 become unreliable. The final result and all intermediate products must stay below 2,147,483,647.

Integer Division Order

The formula result * numerator / i must be calculated in that exact order within a single set /a expression. Batch evaluates left-to-right, so the multiplication happens first, then the division. If you split this into separate statements and divide first, you lose precision due to integer truncation.

Zero Check

By convention, $C(n, 0) = 1$. When r is 0, the for /L loop range becomes (1,1,0) which executes zero iterations, leaving result at its initial value of 1. This handles the edge case automatically.

PowerShell for Large Values​

For combinations involving large numbers, use PowerShell:

@echo off
setlocal

set "n=49"
set "r=6"

for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command ^
"$c = [int64]1; for ($i = 1; $i -le %r%; $i++) { $c = $c * (%n% - $i + 1) / $i }; $c"
`) do set "result=%%A"

echo C(%n%, %r%) = %result%
pause
Why PowerShell?

PowerShell uses 64-bit integers ([int64]) by default for arithmetic, supporting values up to 9,223,372,036,854,775,807. This makes it suitable for combinations like $C(49, 6) = 13,983,816$ or even $C(60, 30)$ that would overflow Batch's 32-bit limit.

Conclusion​

Calculating combinations adds probability and subset-analysis capabilities to your Batch scripts. By using the optimized iterative formula, you avoid the explosive growth of raw factorials and keep your calculations within safe integer limits for a wider range of inputs. This mathematical tool is essential for building decision-support scripts involving team selection, statistical sampling, and resource allocation.