Skip to content
AI360Xpert
Beta
Difficulty: EasyMath & Geometry

Happy Number

Problem in Plain English

Repeatedly replace n with the sum of the squares of its digits, returning true if the chain reaches 1 and false if it falls into a repeating cycle.

Problem Statement

Decide whether a positive integer n is a happy number. Repeatedly replace the number by the sum of the squares of its decimal digits.

Return true if the sequence reaches 1. Return false if it repeats indefinitely without reaching 1. Since the successor of 1 is again 1, reaching that value is enough to finish.

Constraints

  • 1≤n≤231−11 \le n \le 2^{31}-1

Examples

Example 1

Input
n = 19
Output
true

The successors are 12+92=821^2+9^2=82, 82+22=688^2+2^2=68, 62+82=1006^2+8^2=100, and 12+02+02=11^2+0^2+0^2=1. Reaching 1 makes the answer true.

Example 2

Input
n = 1
Output
true

The initial value already equals 1; no transformation is needed. Its successor is also 1.

Example 3

Input
n = 2
Output
false

The chain visits 2, 4, 16, 37, 58, 89, 145, 42, 20, then 4 again. A repeated 4 repeats the same successors forever without reaching 1.

Intuition

The digit-square rule gives each number exactly one successor. For Example 1, the chain is 19 to 82 to 68 to 100 to 1. A repeated value would force every later successor to repeat too, so the real task is detecting a cycle. Since a permitted input has at most ten digits, its first successor is at most 810; every later successor is at most 243. This finite range guarantees eventual repetition, including the happy fixed point 1.

Approaches

Hash Set Cycle

Solution Details

Reveal Hash Set Cycle: intuition, complexity, and code

Hints

Hint 1
Write out where 19 goes (19 to 82 to 68 to 100 to 1) and where 2 goes. What separates the two chains?
Hint 2
Treat each value and its successor as one link in a chain: the chain either ends at 1 or repeats a value forever.
Hint 3
Store every produced value in a set to catch the first repeat, or run a slow pointer one link per round against a fast pointer moving two links and watch whether they meet or the fast one hits 1.

Edge Cases

  • n = 1 is already happy: both loops exit at once and return true.
  • n = 7 is happy through a longer chain (7 to 49 to 97 to 130 to 10 to 1), so chains are not always short.
  • Unhappy numbers like n = 2 never reach 1, so without cycle detection the loop runs forever.

Common Mistakes and Interview Tips

  • Looping until n becomes 1 with no record of seen values, which hangs forever on unhappy inputs such as 2.
  • Advancing the fast pointer by one step instead of two in the Floyd version, so a meeting no longer proves a cycle.
  • Extracting digits with plain / 10 in JavaScript, which keeps fractions; apply Math.floor after each division.
  • Initializing both pointers to n and testing equality before advancing would reject every input except 1. Explain the initial offset and the separate success test in an interview.

Key Takeaway

Any process mapping each value to a next value inside a bounded range must eventually repeat, so cycle tools such as seen sets and fast/slow pointers apply far beyond linked lists.