Teaching Kids Programming – Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)


Teaching Kids Programming: Videos on Data Structures and Algorithms

Introduction to Math Combination

Combinations count the ways to choose items when order does not matter. This guide builds intuition from a simple grid-walking example, introduces binomial notation, derives the formula, explains the recurrence tex_20b70635d529dd6550c4cf1b7166fce1 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial), and connects everything to Pascal’s triangle.

Grid walking example — paths from bottom-left to top-right

Imagine you must move only Right (R) or Up (U). To go from the bottom-left to a point that requires three rights and two ups, every shortest path is a sequence of five moves containing three R’s and two U’s.

combination-grid-walking Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Combination Grid Walking

Each valid path is just a choice of which two positions (out of five) are U (the rest are R). So the number of such paths is “5 choose 2”, written tex_eb50cc17a30bc80a7e9bb243a4000477 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) (which equals tex_e605ee6dcffe94bc93aee83996a214f3 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)).

Example sequences:

R R U R U U R R R U R U R R U R R R U U U U R R R 

The binomial coefficient (combination) notation

The number of ways to choose tex_956cb2a0c12d1c12ff07ffca2950a926 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) items from tex_d26877ae239c15d2776d571f6114453d Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) items (order doesn’t matter) is written

tex_5b51b2b5348c2fe3dbc400879cb17752 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

or

tex_69a402c8e0c6fbb9afd1220af2f4a42e Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Both mean “n choose m”.

Formula for combinations — the factorial derivation

First count ordered selections (permutations): the number of ordered lists of length tex_956cb2a0c12d1c12ff07ffca2950a926 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) from tex_d26877ae239c15d2776d571f6114453d Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) distinct items is

tex_69cfb2451e2eee3b537ff73d0bfcb949 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Each unordered set of tex_52c11c8fd6bfb9574b188e037ac8fcbc Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) items corresponds to tex_33d8268e68aa54dc96647b2775f9cea0 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) ordered lists (the permutations of those tex_52c11c8fd6bfb9574b188e037ac8fcbc Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)). Divide by tex_33d8268e68aa54dc96647b2775f9cea0 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) to get combinations:

tex_d014d28b2f2092c95da4036e39573e34 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Apply the formula to the grid example

For tex_45034f3317e39be77b191a09f521dbc8 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) total steps and tex_bde611fe1120c147cc3116da68c65746 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) ups:

tex_efdda93517094cde5926631915740e2c Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

So there are 10 distinct shortest paths.

Why the formula makes sense intuitively

  • Viewpoint 1 — choose positions: choose the tex_956cb2a0c12d1c12ff07ffca2950a926 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) positions (out of tex_d26877ae239c15d2776d571f6114453d Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)) where the U’s go; that’s tex_5b51b2b5348c2fe3dbc400879cb17752 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial).
  • Viewpoint 2 — divide permutations by ordering: count all permutations of n moves then divide out reorderings of identical moves.

Pascal’s triangle and the recurrence

pascal-triangle Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Pascal Triangle and Combination

Write tex_ba05f2b93f4c2d3a54882c4b098a9fc8 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) in rows to form Pascal’s triangle:

 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 

The entries satisfy the recurrence

tex_a9c4b9f00a6176c3864dff7e2cca9bde Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

Then, we can easily write a top-down dynamic programming implementation using @cache for memoized recursion:

from functools import cache

@cache
def C(n, m):
    if m == 0:
        return 1  # C(n, 0) = 1
    if m == n:
        return 1  # C(n, n) = 1
    return C(n-1, m-1) + C(n-1, m)

Of course, we can also implement it in a bottom-up manner:

def C_bottom_up(n, m):
    dp = [[0]*(m+1) for _ in range(n+1)]
    for i in range(n+1):
        dp[i][0] = 1  # C(i, 0) = 1
        for j in range(1, min(i, m)+1):
            if j == i:
                dp[i][j] = 1  # C(i, i) = 1
            else:
                dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
    return dp[n][m]

This bottom-up implementation accumulates from smaller subproblems to larger ones, avoiding the overhead of recursion and easily extending to compute the entire Pascal’s triangle.

The bottom-up Dynamic Programming for combinations can be further optimized using a one-dimensional array, leveraging the rolling array principle, since each row only depends on the previous row. The key is to update from right to left to avoid overwriting data that hasn’t been used yet.

Here’s an example implementation:

def C_one_dim(n, m):
    dp = [0] * (m+1)
    dp[0] = 1  # C(i, 0) = 1

    for i in range(1, n+1):
        # Update from right to left to avoid overwriting previous row data
        for j in range(min(i, m), 0, -1):
            dp[j] = dp[j] + dp[j-1]
    
    return dp[m]

Example:

print(C_one_dim(5, 2))  # Output: 10

✅ Advantages:

  • Space complexity: O(m)
  • Time complexity: O(n*m)
  • Can easily be extended to compute an entire row or column of combination numbers

Combinatorial proof — picking apples

Want to pick tex_956cb2a0c12d1c12ff07ffca2950a926 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) apples from tex_d26877ae239c15d2776d571f6114453d Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) apples. Consider the last apple (#n):

If you pick it, you must choose the remaining tex_bbb6aed6f41e2c469b0952dd1428ed9c Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) from the first tex_8eaa2e3b994da3c2dd1861cb7c8b99d7 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial): tex_4d0c123c83b77f26eedc67eaa3d43b31 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) ways.

If you skip it, you must choose all tex_956cb2a0c12d1c12ff07ffca2950a926 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) from the first tex_8eaa2e3b994da3c2dd1861cb7c8b99d7 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial): tex_23e36b49b17b39d8b3bd9b084db77675 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) ways.

These two disjoint cases cover all possibilities, so

tex_a9c4b9f00a6176c3864dff7e2cca9bde Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)

(That identity is exactly what builds Pascal’s triangle.)

Grid interpretation of the recurrence

On the grid, look at the last step of any path to a certain point: it is either R or U. Paths ending in R come from one earlier grid point, and paths ending in U come from another. Counting those two groups reproduces the same addition rule.

Common small values and remarks

tex_c560618184dddba94cf9e2345f141963 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) (choose nothing).
tex_d5b33bb299ff86b325995afb4c8f0018 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) (choose one).
tex_9028c423afcf1c38b7c70d5b1b2f6617 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) (choose all).

Small table for tex_45034f3317e39be77b191a09f521dbc8 Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial):

C(5,0)=1
C(5,1)=5
C(5,2)=10
C(5,3)=10
C(5,4)=5
C(5,5)=1 

Final notes

Combinations appear in counting paths, binomial expansions (coefficients of tex_d26877ae239c15d2776d571f6114453d Teaching Kids Programming - Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial)), probability, and selection problems. The factorial formula gives direct computation, while Pascal’s triangle and the recurrence provide inductive intuition and efficient building of values. The grid-walking example is a concrete way to visualize why “choose positions” equals “choose steps”, which is the heart of combinations.

–EOF (The Ultimate Computing & Technology Blog) —

2264 words
Last Post: The Hidden Engine of Performance: It’s All About Where the Data Lives (Cache is the King)
Next Post: Why Parallelism Isn’t Infinite: Amdahl vs Gustafson Explained Simply

The Permanent URL is: Teaching Kids Programming – Introduction to Combinatorial Mathematics 1 (Pascal Triangle/Binomial) (AMP Version)

Leave a Reply