Skip to main content

How to Generate Pascal's Triangle in Batch Script

Pascal's Triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. The edges are always 1. It has deep connections to combinations ($C(n, r)$), probability, and the Binomial Theorem. Generating it in Batch is an excellent exercise in 2D array logic and nested loops.

In this guide, we will demonstrate how to build and display Pascal's Triangle row by row.

The Strategy: The Two-Row Buffer

  1. Start with Row 0: [1].
  2. For each subsequent Row $n$:
    • The first and last elements are always 1.
    • Each inner element is the sum of Row[n-1][j-1] and Row[n-1][j].

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "rows=Enter number of rows: "
set /a "maxIndex=rows - 1"

echo.
echo --- PASCAL'S TRIANGLE (%rows% rows^) ---
echo.

for /L %%n in (0,1,%maxIndex%) do (
set "line="

for /L %%k in (0,1,%%n) do (
if %%k EQU 0 (
set "P_%%n_%%k=1"
) else if %%k EQU %%n (
set "P_%%n_%%k=1"
) else (
set /a "prevRow=%%n - 1"
set /a "prevCol=%%k - 1"

:: Build variable names using intermediate variables
set "varA=P_!prevRow!_!prevCol!"
set "varB=P_!prevRow!_%%k"

:: Use call trick to resolve double-delayed expansion
for %%A in (!varA!) do for %%B in (!varB!) do (
set /a "P_%%n_%%k=!%%A! + !%%B!"
)
)
set "line=!line! !P_%%n_%%k!"
)
echo !line!
)

pause

Example of output with 10 rows:

Enter number of rows: 10

--- PASCAL'S TRIANGLE (10 rows) ---

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
How the Variable Resolution Works

Batch does not support nested delayed expansion like !P_!prevRow!_!prevCol!!. Instead, we build the variable name as a string (e.g., varA=P_3_1), then use a for loop to transfer that string into a loop variable (%%A), and finally resolve it with !%%A!. This is a standard Batch pattern for double-indirection.

Why Generate Pascal's Triangle?

  1. Combinatorics: The $n$th row, $k$th position gives you $C(n, k)$ directly. This is a visual way to compute combinations without the factorial formula.
  2. Probability: Pascal's Triangle is used to calculate binomial probabilities (e.g., the probability of getting exactly 3 heads in 5 coin flips).
  3. Pattern Discovery: The triangle contains hidden patterns, including Fibonacci numbers (diagonal sums), powers of 2 (row sums), and powers of 11 (row concatenation for small rows).

Important Limitations

32-Bit Overflow

Values in Pascal's Triangle grow quickly. Row 34 contains values exceeding 2.1 billion, making it the practical limit for Batch. Beyond this, set /a silently overflows and produces incorrect results.

Display Alignment

The script above displays values left-aligned. For a true "triangle" shape with centered rows, you would need to add leading spaces proportional to (totalRows - currentRow) before each line.

Memory Usage

Each cell is stored as a separate environment variable. For 30 rows, this creates about 465 variables, well within Batch's limits. However, you only need the previous row to calculate the current row. Instead of storing all rows, you can use two buffers (PREV_ and CURR_) and swap them after each row to reduce memory usage.

Best Practices

  1. Memory Optimization: You only need the previous row to calculate the current row. Instead of storing all rows, you can use two buffers (PREV_ and CURR_) and swap them after each row.
  2. Centering: To visually center the triangle, add (totalRows - currentRow) spaces before each line.

Conclusion

Generating Pascal's Triangle is a beautiful demonstration of mathematical patterns brought to life through code. By combining 2D array logic with simple addition, you create a structure that reveals deep connections between numbers. This exercise strengthens your mastery of nested loops and variable namespacing, providing the skills needed to build complex mathematical visualizations and analysis tools in Batch.