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.
Table of Content
[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:
- Sum the digits: Start by adding all the digits of the given number.
- Check the result: If the sum is a single-digit number (i.e., less than 10), stop and return it.
- 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.
#include <iostream>
using namespace std;
// Function to find the digital root
int digitalRoot(int n)
{
int res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9)
{
// If n becomes 0, reset it to res
// and start a new iteration.
if (n == 0)
{
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
// Driver Code
int main()
{
int n = 99999;
cout << digitalRoot(n);
return 0;
}
import java.util.Scanner;
// Function to find the digital root
public class GfG {
public static int digitalRoot(int n)
{
int res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9) {
// If n becomes 0, reset it to res
// and start a new iteration.
if (n == 0) {
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int n = 99999;
System.out.println(digitalRoot(n));
}
}
"""
Function to find the digital root
"""
def digitalRoot(n):
res = 0
# Repetitively calculate sum until
# it becomes single digit
while n > 0 or res > 9:
# If n becomes 0, reset it to res
# and start a new iteration.
if n == 0:
n = res
res = 0
res += n % 10
n //= 10
return res
# Driver Code
if __name__ == "__main__":
n = 99999
print(digitalRoot(n))
using System;
public class GfG {
public int digitalRoot(int n)
{
int res = 0;
while (n > 0 || res > 9) {
if (n == 0) {
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
public static int Main()
{
int n = 99999;
GfG obj = new GfG();
Console.WriteLine(obj.digitalRoot(n));
return 0;
}
}
// Function to find the digital root
function digitalRoot(n)
{
let res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9) {
// If n becomes 0, reset it to res
// and start a new iteration.
if (n === 0) {
n = res;
res = 0;
}
res += n % 10;
n = Math.floor(n / 10);
}
return res;
}
// Driver Code
let n = 99999;
console.log(digitalRoot(n));
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 + 0This 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.
#include <iostream>
using namespace std;
int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
int main()
{
int n = 99999;
cout << digitalRoot(n);
return 0;
}
#include <stdio.h>
int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
int main()
{
int n = 99999;
printf("%d", digitalRoot(n));
return 0;
}
public class GfG {
public static int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
public static void main(String[] args)
{
int n = 99999;
System.out.println(digitalRoot(n));
}
}
def digitalRoot(n):
# If given number is zero its
# digit sum will be zero only
if n == 0:
return 0
# If result of modulo operation is
# zero then, the digit sum is 9
if n % 9 == 0:
return 9
return n % 9
if __name__ == "__main__":
n = 99999
print(digitalRoot(n))
using System;
public class Gfg {
public int digitalRoot(int n)
{
return 1 + (n - 1) % 9;
}
public static void Main()
{
int n = 99999;
Gfg obj = new Gfg();
Console.WriteLine(obj.digitalRoot(n));
}
}
function digitalRoot(n)
{
// If given number is zero its
// digit sum will be zero only
if (n === 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 === 0)
return 9;
return (n % 9);
}
let n = 99999;
console.log(digitalRoot(n));
Output
9
Time Complexity: O(1)
Auxiliary Space: O(1)