How to Calculate Permutations (nPr) in Batch Script
A Permutation counts the number of ways to arrange $r$ items from a set of $n$ items, where the Order Matters. The formula is: $P(n, r) = n! / (n - r)!$. For example, $P(5, 3) = 5! / 2! = 120 / 2 = 60$. Permutations are used in probability, scheduling, and generating unique arrangements.
In this guide, we will demonstrate how to calculate permutations using direct multiplication (a partial factorial).
The Strategy: The Partial Factorialβ
Instead of computing two full factorials and dividing them (which risks overflow), calculate nPr directly by multiplying:
[ n \times (n-1) \times (n-2) \times \ldots ]
for exactly r terms.
Implementation Scriptβ
@echo off
setlocal enabledelayedexpansion
set /p "n=Enter n (total items): "
set /p "r=Enter r (items to arrange): "
:: Validate: numeric only (no blanks, no signs, no decimals)
if not defined n goto :bad
if not defined r goto :bad
for /f "delims=0123456789" %%A in ("!n!") do goto :bad
for /f "delims=0123456789" %%A in ("!r!") do goto :bad
:: Validate: r <= n
if !r! GTR !n! (
echo [ERROR] r cannot be greater than n.
pause
exit /b
)
:: Calculate nPr by multiplying n * (n-1) * ... for r terms
set /a "result=1"
set /a "current=n"
for /L %%i in (1,1,!r!) do (
set /a "result*=current"
set /a "current-=1"
)
echo.
echo ==========================================
echo P(!n!, !r!) = !result!
echo ==========================================
pause
exit /b
:bad
echo.
echo [ERROR] Enter whole numbers only (0-9), e.g. n=10 and r=3.
pause
exit /b
This direct multiplication approach avoids computing n! and (n-r)! separately, which overflows sooner. Itβs mathematically equivalent to the factorial formula but more practical in Batch.
Why Calculate Permutations?β
- Password Combinations: How many unique 4-character passwords can be made from 10 possible characters (no repeats)? $P(10, 4) = 5040$.
- Task Scheduling: In how many different orders can 3 tasks be assigned to 3 specific workers? $P(3, 3) = 6$.
- Tournament Brackets: How many possible orderings exist for the top 3 finishers out of 10 competitors?
Important Limitationsβ
- 32-Bit Overflow: Batch arithmetic is 32-bit signed. Values above
2147483647overflow. Since results grow quickly,nPris only reliable for smalln(often roughlyn β€ 12, depending onr). - No Decimals: This is whole-number math only.
- Zero Check: By convention, $P(n, 0) = 1$. The loop naturally handles this because it runs 0 times and leaves
resultas 1.
Even if n is small, some combinations overflow sooner than you might expect. For example, P(13, 13) = 13! overflows 32-bit arithmetic.
PowerShell for Large Valuesβ
For large values, delegate to PowerShell (which can use big integers):
@echo off
set "n=20" & set "r=5"
powershell -NoProfile -Command ^
"$n=%n%; $r=%r%; if($r -gt $n){throw 'r>n'}; $p=[bigint]1; for($i=$n; $i -gt ($n-$r); $i--){$p*=$i}; $p"
pause
Never pass unvalidated user input directly into PowerShell commands. If n/r are user-provided, validate them first (digits-only and range checks) to avoid command injection.
Conclusionβ
Calculating permutations allows your scripts to model real-world arrangement problems. By using the "Partial Factorial" multiplication approach, you avoid unnecessary computation and keep your values within safe boundaries for longer. This mathematical capability enables you to build scheduling, probability, and analysis tools directly within the Windows command-line environment.