Loading
Loading
A puzzle app shows a number, then replaces it with the sum of the squares of its digits, again and again. A number wins a gold star if the chain eventually reaches 1. Otherwise the chain falls into a loop that never includes 1.
Given a positive integer n, return true if its chain reaches 1, and false if it loops forever.
n = 19191² + 9² = 82828² + 2² = 68686² + 8² = 1001001² + 0² + 0² = 11settledtrueSign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
19 → 82 → 68 → 100 → 1.
Example 2
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 loops forever.
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(log n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.