Given a positive integer n, return its magical number.
The magical number is found by adding the digits of n, then the digits of that sum, and so on, until only one digit is left. A number with one digit is its own magical number.
Example 1:
Input: n = 39 Output: 3 Explanation: 39 → 3 + 9 = 12 → 1 + 2 = 3
Example 2:
Input: n = 928435 Output: 4 Explanation: 928435 → 9 + 2 + 8 + 4 + 3 + 5 = 31 → 3 + 1 = 4
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
