Problem Solving — CS & DS

Lecture 20 — Brute Force and When It Wins

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The 4-Digit Lock: ten thousand tries and a theorem

cs-077

How many 4-digit codes (0000 to 9999) have digit sum exactly 12?

count = 0
for code in range(10000):
    if sum(int(d) for d in f"{code:04d}") == 12:
        count += 1
print(count)

Expected: 415 codes

Cost: O(10^4 x 4) - constant here, but the pattern is exponential in the number of dials: k dials -> 10^k. Brute force wins while the space stays small.

Worked example

Perfect Numbers: brute force with a sqrt speed-up

cs-078

A perfect number equals the sum of its proper divisors (6 = 1+2+3). Find all perfect numbers below 10000.

def is_perfect(n):
    total = 1
    d = 2
    while d * d <= n:
        if n % d == 0:
            total += d + n // d
        d += 1
    return n > 1 and total == n

print([n for n in range(2, 10000) if is_perfect(n)])

Expected: [6, 28, 496, 8128]

Cost: O(n * sqrt(n)) overall - about 1.3 million operations here; the naive version needs 50 million. Same answer, 40x cheaper.

Worked example

The Subset Riddle: every subset, systematically

cs-080

From values 3, 5, 2, 8, find a subset summing to 10 - or prove none exists.

items = [3, 5, 2, 8]
target = 10
found = None
for mask in range(1 << len(items)):
    total = sum(items[i] for i in range(len(items)) if mask >> i & 1)
    if total == target:
        found = [items[i] for i in range(len(items)) if mask >> i & 1]
        break
print(found)

Expected: subset found: 3 + 5 + 2 = 10

Cost: O(2^n) time - the price of the proof; meet-in-the-middle or DP (later lectures) trade that certainty for speed.

Case attempt · lecture 20
Case cs-077 · Beginner

The 4-Digit Lock

Enumerate all 4-digit PINs satisfying 'digits sum to 12'. Count the space first, then enumerate. When brute force is the whole solution.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-077 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-077 — not embedded in this deck.

Case attempt · lecture 20
Case cs-078 · Foundational

Perfect Numbers

Find all perfect numbers below 10000 (equal to sum of proper divisors). Brute force with a smarter divisor bound; measure the improvement.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-078 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-078 — not embedded in this deck.

Case attempt · lecture 20
Case cs-079 · Intermediate

The Meeting Triple

Find three people who can all attend the same slot from availability masks. Enumerate triples systematically and prune; estimate cost honestly.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-079 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-079 — not embedded in this deck.

Case attempt · lecture 20
Case cs-080 · Advanced

The Subset Riddle

Pick numbers from a list to hit an exact target sum. Full enumeration is 2^n; show the cost curve, solve small n, then sketch why cleverness is needed for big n.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-080 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-080 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. How big is the subset space of 25 items, roughly?
  2. What makes pruning sound?
  3. When is brute force the right deliverable?
Expected answers (instructor): 2^25 ≈ 33 million — borderline; count before you loop it only removes branches that cannot contain a better answer tiny spaces, correctness baselines, and verifying clever answers
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF