Problem
You get a positive integer n. Repeat this step on it: replace the number with the sum of the squares of its digits.
The number n is happy if the steps reach 1. If the steps never reach 1, the numbers repeat in a loop forever. Return true if n is happy. Otherwise, return false.
Example 1
- Input:
- n = 13
- Output:
- true
- Explanation:
- The first step gives
1^2 + 3^2, which is 10. The next step gives1^2 + 0^2, which is 1.
Example 2
- Input:
- n = 4
- Output:
- false
- Explanation:
- The steps give 16, 37, 58, 89, 145, 42, 20 and then 4 again. This loop never reaches 1.
Constraints
- 1 <= n <= 2^31 - 1
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Linked List Cycle
Coding · Floyd's Cycle Finding Algorithm · Hash Table · Linked List
- Count Number of Nice Subarrays
Coding · Array · Hash Table · Math
- Fraction to Recurring Decimal
Coding · Hash Table · Math · String
- Insert Delete GetRandom O(1)
Coding · Array · Design · Hash Table
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Snowflake
Leetcode 202. Happy Number
Reported May 23, 2026 · 4 source reports
JUNIOR, MID_LEVEL
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 1 record
Snowflake
Happy Number
Reported date not specified · Source window: 6mo
Source snapshot: Oct 20, 2026
Source frequency score: 72.2. This is not an interview probability.
View source