Digit and Number Math
What is this?
These tricky problems look like they need a loop or simulation, but each one hides a number identity that makes the loop disappear. The single insight is to stop simulating and count the right thing โ a digital root that's just a value mod 9, a count of one prime factor, or a single greedy digit swap. Recognizing the closed-form shortcut is the whole game.
๐ก Fun fact: Repeatedly summing a number's digits until one remains always equals the number modulo 9 โ because every power of ten leaves a remainder of 1 when divided by 9, so the digits and the whole number are congruent mod 9.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: Many "number" problems hide a closed-form identity. Instead of simulating the process, you spot the underlying arithmetic โ a mod-9 root, a count of a single prime factor, a greedy digit choice โ and the loop collapses to a formula or a single pass.
The pattern
The reframe here is stop simulating, start counting the right thing. Repeatedly summing digits until one remains is just the value modulo 9 (the digital root), because each power of ten is congruent to 1 mod 9. The whole iterative process becomes one expression.
The same instinct applies to factorials: trailing zeros come from factors of ten, and tens are scarcer in their 5 than their 2, so you only count how many 5s the factorial contributes โ including the extra 5s from 25, 125, and so on. You never build the huge number.
For rearranging digits, the move is greedy with the right target: scan for the leftmost digit that can be improved, and swap it with the largest later digit (preferring the rightmost occurrence of that largest digit to maximize the gain). One swap, chosen carefully, beats searching all pairs. The thread throughout: find the identity or the single decisive move.
The problems
- Add Digits โ the repeated digit sum is the digital root, computable as
1 + (n-1) % 9for positiven; no looping. - Factorial Trailing Zeroes โ count factors of 5 across the factorial (
n/5 + n/25 + n/125 + ...), since 2s always outnumber 5s. - Maximum Swap โ greedily find the leftmost digit smaller than some later digit, then swap it with the rightmost occurrence of the largest such digit.
Key takeaways
- Look for the identity โ digital root is mod 9, not a loop.
- Count the scarce factor โ trailing zeros track 5s, not 10s.
- Greedy needs the right target โ leftmost improvable, rightmost-largest.
- Don't build the big number โ reason about its factors instead.
Rectangle Area
Core idea: The area covered by two overlapping rectangles isn't the sum of their areas โ the shared region would be counted twice. So compute
area1 + area2 โ overlap, where the overlap is itself a rectangle whose width and height you clamp to0the moment the two shapes stop touching.
Problem, rephrased
Imagine you're laying out two sticky notes on a whiteboard. Each note is an axis-aligned rectangle โ its sides are parallel to the x and y axes โ described by two corners: its bottom-left (ax1, ay1) and its top-right (ax2, ay2).
You drop the second note somewhere, and it may land clear of the first, partly on top of it, or completely inside it. The question: how much whiteboard surface is hidden under paper? That is, the area of the union of the two rectangles โ every point covered by at least one note, with the shared region counted exactly once.
Formally: given rectangle one as (ax1, ay1, ax2, ay2) and rectangle two as (bx1, by1, bx2, by2), return the total area covered by the two rectangles together.
| Rectangle 1 (bl, tr) | Rectangle 2 (bl, tr) | Output | Why |
|---|---|---|---|
(-3,0)โ(3,4) |
(0,-1)โ(9,2) |
45 | 24 + 27 โ 6 overlap |
(-2,-2)โ(2,2) |
(3,3)โ(5,5) |
20 | 16 + 4 โ 0 (disjoint, no overlap) |
(0,0)โ(10,10) |
(2,2)โ(5,5) |
100 | second is fully inside the first |
(0,0)โ(0,0) |
(0,0)โ(0,0) |
0 | two degenerate (zero-area) rectangles |
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson