Math Fundamentals18 sections · 814 units
Open in Course

Practice - Happy Number

LeetCode 202

A happy number is defined as: start with any positive integer, replace it with the sum of the squares of its digits, and repeat until the number equals 11 or loops endlessly in a cycle that doesn't include 11.

For example, 1919 is happy: 12+92=821^2 + 9^2 = 82, 82+22=688^2 + 2^2 = 68, 62+82=1006^2 + 8^2 = 100, 12+02+02=11^2 + 0^2 + 0^2 = 1.

Write a function that determines if nn is happy. Use digit extraction (modulo and division) and cycle detection.