Ctrl K

Digit Squares Down to One

Easy
Take a positive integer n and keep applying one step: replace the number with the sum of the squares of its digits. For example, 23 becomes 2² + 3² = 13. Sooner or later the sequence either reaches 1 (and stays there) or falls into a loop that never contains 1. Return True if starting from n you eventually reach 1, and False otherwise.
Example 1
n=7 → True

7 → 49 → 16 + 81 = 97 → 81 + 49 = 130 → 1 + 9 + 0 = 10 → 1. We reach one.

Example 2
n=4 → False

4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4: the sequence is back at 4 and will keep cycling without ever hitting 1.

Example 3
n=1 → True
Constraints
  • 1 ≤ n ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Equals a Pair Product MediumValue of a Roman Numeral EasySame When Flipped EasyThe Lost Ticket EasyValues in Both Lists Easy
loading Python…
Input
n=7
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 56 tests on the server, hidden ones included.