Sum of Digits in Base K - Problem
Sum of Digits in Base K

You're given an integer n (in base 10) and a target base k. Your task is to convert the number n from base 10 to base k, then sum all the digits of the converted number.

Here's the interesting part: after converting to base k, treat each digit as a regular base-10 number when calculating the sum. For example, if converting 34 to base 6 gives you "54", you sum 5 + 4 = 9.

Goal: Return the sum of digits after base conversion.
Input: An integer n (base 10) and base k
Output: Sum of digits in the base-k representation (as base 10)

Input & Output

example_1.py โ€” Basic Conversion
$ Input: n = 34, k = 6
โ€บ Output: 9
๐Ÿ’ก Note: 34 in base 6 is 54 (since 34 = 5ร—6ยน + 4ร—6โฐ). Sum of digits: 5 + 4 = 9
example_2.py โ€” Binary Conversion
$ Input: n = 10, k = 2
โ€บ Output: 2
๐Ÿ’ก Note: 10 in base 2 is 1010 (since 10 = 1ร—2ยณ + 0ร—2ยฒ + 1ร—2ยน + 0ร—2โฐ). Sum of digits: 1 + 0 + 1 + 0 = 2
example_3.py โ€” Edge Case
$ Input: n = 0, k = 10
โ€บ Output: 0
๐Ÿ’ก Note: 0 in any base is still 0. Sum of digits: 0

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 2 โ‰ค k โ‰ค 10
  • n will be converted to base k where each digit is < k

Visualization

Tap to expand
Base Conversion: 34 โ†’ Base 634Original (base 10)รท 65 R 4quotient remainder4sum = 45Continue with 5รท 60 R 5quotient remainder5sum = 4+5=9Final Answer: 934โ‚โ‚€ = 54โ‚† โ†’ 5 + 4 = 9
Understanding the Visualization
1
Initialize
Start with your number n and target base k
2
Divide & Collect
Divide n by k, add remainder to sum, update n
3
Repeat
Continue until n becomes 0
4
Result
The sum contains our answer
Key Takeaway
๐ŸŽฏ Key Insight: Base conversion uses repeated division - the remainders become our digits, and we sum them as we go!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.7K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen