Sum of Digits in Base K

Here is a simple approach to solve the problem:

  1. First, we need to convert the given number from base 10 to base k. This can be done by repeatedly dividing the number by k and appending the remainder to the start of our result string.
  2. After that, we iterate over each character of the result string, convert them to integers, and add them together.

Let’s implement this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def sumBase(self, n: int, k: int) -> int:
        base_k = ''
        while n:
            base_k = str(n % k) + base_k
            n //= k

        total_sum = 0
        for digit in base_k:
            total_sum += int(digit)

        return total_sum

The while loop is used to convert the number from base 10 to base k. The for loop is used to calculate the sum of the digits. This solution works within the provided constraints and should give the correct answer.