Happy Number
Real-world version: Probe Reading SettlesThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def reading_settles(reading: int) -> bool:public boolean readingSettles(int reading)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's