#1837

Sum of Digits in Base K

newbie · 195 · lc easy +16 · verified · 78.6% accepted · 559 likes · top 90%

Description

Given a non-negative integer n in base 10 and a base k, convert n to base k and return the sum of its digits (treating each digit as a base-10 value, with the final sum returned in base 10).

Example 1:

Input: n = 34, k = 6
Output: 9
Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Example 2:

Input: n = 10, k = 10
Output: 1
Explanation: n is already in base 10. 1 + 0 = 1.

Code

1
2
3