Program to find super digit of a number in Python

Suppose we have a number n. We have to find the super digit of this number. The super digit of a single digit number is the digit itself, but for multi-digit numbers, super digit is the sum of all digits repeatedly until the sum is a single digit number.

So, if the input is like n = 513682, then the output will be 7 because (5+1+3+6+8+2) = 25, (2 + 5) = 7.

Algorithm

To solve this, we will follow these steps ?

  • s := 0
  • while n > 0 or s > 9, do
    • if n is same as 0, then
      • n := s
      • s := 0
    • s := s + n mod 10
    • n := floor value of n/10
  • return s

Method 1: Using While Loop

This approach processes each digit one by one and calculates the sum repeatedly ?

def solve(n):
    s = 0
    while(n > 0 or s > 9):
        if n == 0:
            n = s
            s = 0
        
        s += n % 10
        n //= 10
    
    return s

n = 513682
print(f"Super digit of {n} is: {solve(n)}")
Super digit of 513682 is: 7

Method 2: Using String Conversion

A more intuitive approach that converts the number to string and sums the digits ?

def super_digit_string(n):
    while n >= 10:
        digit_sum = sum(int(digit) for digit in str(n))
        n = digit_sum
    return n

# Test with different numbers
numbers = [513682, 123, 999, 7]
for num in numbers:
    result = super_digit_string(num)
    print(f"Super digit of {num} is: {result}")
Super digit of 513682 is: 7
Super digit of 123 is: 6
Super digit of 999 is: 9
Super digit of 7 is: 7

Method 3: Mathematical Formula (Digital Root)

The super digit is also known as the digital root, which can be calculated using a mathematical formula ?

def digital_root(n):
    if n == 0:
        return 0
    return 1 + (n - 1) % 9

# Test the formula approach
numbers = [513682, 123, 999, 7, 0]
for num in numbers:
    result = digital_root(num)
    print(f"Digital root of {num} is: {result}")
Digital root of 513682 is: 7
Digital root of 123 is: 6
Digital root of 999 is: 9
Digital root of 7 is: 7
Digital root of 0 is: 0

Comparison

Method Time Complexity Space Complexity Best For
While Loop O(log n) O(1) Step-by-step understanding
String Conversion O(log n) O(log n) Readable code
Mathematical Formula O(1) O(1) Performance

Conclusion

The super digit (digital root) can be calculated using iterative digit summation or the mathematical formula 1 + (n - 1) % 9. The formula approach is most efficient with O(1) time complexity, while iterative methods help understand the concept better.

Updated on: 2026-03-26T15:48:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements