◀ THE GRIND — MATH & GEOMETRY

Happy Number

The drill: Repeatedly replace a number with the sum of the squares of its digits and watch where it goes: some numbers eventually settle on 1, others fall into a repeating loop that never reaches it. Decide which fate this one has.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A positive integer arrives, and the drill repeatedly replaces it with the sum of the squares of its own digits, watching where that chain of replacements eventually leads.

Some starting numbers eventually land on exactly 1 and stay there — those are the ones the drill calls happy. Others fall into a repeating cycle of values that never includes 1 at all, looping forever instead of settling.

The answer is a single true or false: true the moment the chain reaches 1, false once it becomes clear the chain has looped back into a cycle it can never escape.

EX 01
n = 1
true
ALREADY 1
EX 02
n = 2
false
SMALLEST NUMBER THAT LOOPS
EX 03
n = 7
true
REACHES 1 AFTER SEVERAL STEPS
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Every number under about a billion collapses within a few steps to something small — so the sequence either reaches 1, or it must eventually repeat a value it's already visited. There's no third option.

HINT 2 THE STRUCTURE

Remembering every value seen so far settles the question the moment a repeat shows up — but that memory grows with the walk. Two runners at different speeds can detect the same repeat without remembering anything at all.

HINT 3 ONE STEP FROM THE ANSWER

Run a slow pointer one digit-square-sum step at a time and a fast pointer two steps at a time, exactly like cycle detection on a linked list. If they ever meet on a value other than 1, the number loops forever; if either hits 1, it's happy.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE TWO-SPEED CHASEPATTERN · SLOW/FAST CYCLE DETECTIONn = 7
7
49
97
130
10
1
STEP 1

Start the digit-square-sum chain at 7. Slow moves one step at a time; fast takes its head start of two.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/happy-number.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def isHappy(self, n: int) -> bool:
        def next_value(x: int) -> int:
            return sum(int(d) ** 2 for d in str(x))

        slow, fast = n, next_value(n)
        while fast != 1 and slow != fast:
            slow = next_value(slow)
            fast = next_value(next_value(fast))
        return fast == 1
TIME O(LOG N)SPACE O(1)PYTHON · RACE PACE · 10 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED