VOOZH about

URL: https://www.geeksforgeeks.org/dsa/digital-rootrepeated-digital-sum-given-integer/

⇱ Digital Root (repeated digital sum) of the given large integer - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Digital Root (repeated digital sum) of the given large integer

Last Updated : 2 Jun, 2026

You are given a number n, you need to find the digital root of n.

  • The digital root of a positive integer is found by summing the digits of the integer.
  • If sum of digits is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated.
  • This is continued as long as necessary to obtain a single digit.

Examples :

Input: n = 1
Output:  1
Explanation: Digital root of 1 is 1.

Input: n = 99999
Output: 9
Explanation: The sum of digits of 99999 is 45 which is not a single digit number, hence the sum of digits of 45 is 9 which is a single digit number.

[Naive Approach] Repetitively Adding Digits -  O(d) Time and O(1) Space

The approach is focused on calculating the digital root of a number, which is the result of summing the digits repeatedly until a single-digit value is obtained. Here's how it works conceptually:

  1. Sum the digits: Start by adding all the digits of the given number.
  2. Check the result: If the sum is a single-digit number (i.e., less than 10), stop and return it.
  3. Repeat the process: If the sum is still more than a single digit, repeat the process with the sum of digits. This continues until a single-digit sum is reached.

Example:

For a number like 1234

  • First, sum the digits: 1 + 2 + 3 + 4 = 10.
  • Since 10 is not a single-digit number, sum its digits: 1 + 0 = 1.
  • Now, 1 is a single-digit number, so we stop and return 1.

Output
9

Time Complexity: O(d), where ddd is the number of digits in nnn.
Auxiliary Space: O(1)

[Expected Approach] Using Mathematical Formula - O(1) Time O(1) Space

We know that every number in the decimal system can be expressed as a sum of its digits multiplied by powers of 10. For example, a number represented as abcd can be written as follows:

abcd = a * 10 ^ 3 + b * 10 ^ 2 + c * 10 ^ 1 + d * 10 ^ 0

We can separate the digits and rewrite this as:
abcd = a + b + c + d + (a * 999 + b * 99 + c * 9)
abcd = a + b + c + d + 9 * (a * 111 + b * 11 + c)

This implies that any number can be expressed as the sum of its digits plus a multiple of 9.
So, if we take modulo with 9 on both side,
abcd % 9 = (a + b + c + d) % 9 + 0

This means that the remainder when abcd is divided by 9 is equal to the remainder where the sum of its digits (a + b + c + d) is divided by 9.

If the sum of the digits itself consists of more than one digit, we can further express this sum as the sum of its digits plus a multiple of 9. Consequently, taking modulo 9 will eliminate the multiple of 9 until the sum of digits becomes a single-digit number.

As a result, the digital root of a number can be found using modulo 9. If the result of the modulo operation is zero for a non-zero number, then the digital root is 9.

  • digitalRoot(n) = 0, if n = 0
  • digitalRoot(n) = 9, if n % 9 = 0 and n > 0
  • digitalRoot(n) = n % 9, otherwise.

Output
9

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment