Start learning free

LESSON 279 OF 287 · FUNCTIONS & CLASSES

Magical Number

Practice O(log n)

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

STEP 1 OF 9
    Ctrl + Enter to check