Skip to content
LC-1837 Easy LeetCode

1837. Sum of Digits in Base K

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 78% Topics: Math
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

class Solution(object):
    def sumBase(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: int
        """
        result = 0
        while n:
            n, r = divmod(n, k)
            result += r
        return result

Solution from kamyu104/LeetCode-Solutions · MIT