Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0258 - Add Digits
    0258 - Add Digits
    0258 - Add Digits
    0258 - Add Digits

    0258 - Add Digits

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/add-digits/

    Solution 1 - Simulation (Loop)

    Given an integer num, repeatedly sum its digits until only one digit remains. The straightforward approach is to simulate this process: extract each digit, sum them, and repeat until the result is a single digit.

    TimeComplexity:O(log⁡2n)TimeComplexity: O(\log^2 n)TimeComplexity:O(log2n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Digital Root Formula (O(1))

    The repeated digit sum of a number is known as its digital root. There is a well-known mathematical formula: for any non-negative integer num, the digital root is 1 + (num - 1) % 9 when num > 0, and 0 when num == 0. This avoids all loops and runs in constant time.

    TimeComplexity:O(1)TimeComplexity: O(1)TimeComplexity:O(1)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0258.1 - Solution 1 - Simulation (Loop) """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def addDigits(self, num: int) -> int:
            """Add Digits Function"""
            while num >= 10:
                digit_sum = 0
                while num > 0:
                    digit_sum += num % 10
                    num //= 10
                num = digit_sum
            return num
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().addDigits(38))   # Expected: 2
        print(Solution().addDigits(0))    # Expected: 0
        print(Solution().addDigits(123))  # Expected: 6
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0258.2 - Solution 2 - Digital Root Formula """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def addDigits(self, num: int) -> int:
            """Add Digits Function"""
            if num == 0:
                return 0
            return 1 + (num - 1) % 9
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().addDigits(38))   # Expected: 2
        print(Solution().addDigits(0))    # Expected: 0
        print(Solution().addDigits(123))  # Expected: 6
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()