EasyMath · Fast and slow pointers

Non-Cyclical Number

Replace a positive integer by the sum of the squares of its digits, again and again. If this eventually reaches 1, the number is non-cyclical (return true); if it loops forever in a cycle without 1, return false. (Also known as "Happy Number".)

100 → 1² + 0² + 0² = 1 → true101 → 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 … → false

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Number chain

Each arrow is one sumOfSquares step; the chain ends in 1 or in a loop

Time and space complexity

Here n is the input number. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Enter any n from 1 to 1000.