Skip to main content

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
tip

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?​

  1. Password Combinations: How many unique 4-character passwords can be made from 10 possible characters (no repeats)? $P(10, 4) = 5040$.
  2. Task Scheduling: In how many different orders can 3 tasks be assigned to 3 specific workers? $P(3, 3) = 6$.
  3. Tournament Brackets: How many possible orderings exist for the top 3 finishers out of 10 competitors?

Important Limitations​

  1. 32-Bit Overflow: Batch arithmetic is 32-bit signed. Values above 2147483647 overflow. Since results grow quickly, nPr is only reliable for small n (often roughly n ≀ 12, depending on r).
  2. No Decimals: This is whole-number math only.
  3. Zero Check: By convention, $P(n, 0) = 1$. The loop naturally handles this because it runs 0 times and leaves result as 1.
warning

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
danger

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.