Math Fundamentals18 sections · 814 units
Open in Course

Practice - Happy Number

(LeetCode 202)

A happy number is defined by repeatedly replacing the number by the sum of the squares of its digits until it equals 1, or loops endlessly in a cycle.

Example: 19 is happy. 1^2 + 9^2 = 82. 8^2 + 2^2 = 68. 6^2 + 8^2 = 100. 1^2 + 0^2 + 0^2 = 1.

Use mod to extract digits and detect cycles with a set.