All problems
0064EasyHash TableMathTwo PointersFloyd's Cycle Finding Algorithm

Probe Reading Settles

Tracked in this browser only
Write code

Trains the technique from

LeetCode 202Happy Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A field probe stores one positive integer reading and runs a self-test on it.

On each tick of the self-test the firmware throws away the stored value and writes back a new one: take every decimal digit of the old value, square each digit, and add those squares together.

The probe declares itself healthy as soon as the stored value becomes 1, because 1 maps to itself and the value can never move again. For every other starting value that never lands on 1, the firmware walks around a fixed ring of values and keeps circling it forever.

Given the starting value reading, return true when the self-test eventually declares the probe healthy, and false when the firmware circles forever.

Examples

Example 1

Input
reading = 7
Output
true

The stored value moves 7 -> 49 -> 97 -> 130 -> 10 -> 1, so the probe declares itself healthy on the fifth tick.

Example 2

Input
reading = 4
Output
false

The value walks 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 and is back where it began, so it will circle that ring forever.

Example 3

Input
reading = 1000
Output
true

Only one digit is non-zero, so the very first tick stores 1 and the probe is healthy immediately.

Constraints

  • 1 <= reading <= 2^31 - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def reading_settles(reading: int) -> bool:
Java
public boolean readingSettles(int reading)
September 7
Apply