#1837
Easy Algorithms Sum of digits in base k
Math
78.5% acceptance
Feb 25, 2026
558
50
Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.
After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn sum_base(mut n: i32, k: i32) -> i32 {
let mut sum = 0;
while n > 0 {
sum += n % k;
n /= k;
}
sum
}
}