🧮

Counting Integer Partitions

Sep 19, 2025

Overview

This lecture explains the problem of counting integer partitions using recursion, focusing on the recursive strategy and implementation for the count_partitions function.

Counting Integer Partitions

  • A partition of n using parts up to size m is a way to express n as a sum of positive integers ≤ m in increasing order.
  • For example, count_partitions(6, 4) counts all ways to write 6 using parts of size 1, 2, 3, or 4, in increasing order.
  • The function does not return the partitions themselves, but the number of possible partitions.

Recursive Decomposition Strategy

  • The problem can be divided into two cases: partitions including at least one m, and partitions with only smaller parts (< m).
  • If at least one m is used, partition n - m using parts up to size m.
  • If no m is used, partition n using parts up to size m - 1.
  • The total number of partitions is the sum of these two cases.
  • This recursive approach creates a tree recursion structure, exploring all possibilities.

Base Cases and Implementation

  • Base case 1: If n == 0, there is one way to partition (use no parts).
  • Base case 2: If n < 0, there are zero ways to partition (invalid partition).
  • Base case 3: If m <= 0, there are zero ways to partition (no valid parts left).
  • Implementation uses the recursive approach by summing the two cases and handling the base cases.

Example: count_partitions(5, 3)

  • The function explores all ways to partition 5 using 1, 2, or 3 as part sizes.
  • Recursive calls break the problem into simpler subproblems until reaching base cases.
  • The sum of valid alternatives gives the total number of partitions.

Key Terms & Definitions

  • Partition — An expression of a number as a sum of positive integers in increasing order.
  • Tree Recursion — A recursive structure where each call can branch into multiple subcalls, used here to handle alternative partition choices.

Action Items / Next Steps

  • Review the recursive decomposition for count_partitions.
  • Practice implementing count_partitions in code.
  • Try examples such as count_partitions(6, 4) and count_partitions(5, 3) to solidify understanding.