Sum of Digits Across All Bases from 2 to K
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Tests ability to repeatedly convert an integer into multiple bases and accumulate digit sums, combining base-conversion mechanics with modular arithmetic.
Sum of Digits Across All Bases
Given a positive integer n and an integer k (with k >= 2), compute the total digit sum of n when written in every base b for b = 2, 3, 4, …, k.
More precisely, for each base b from 2 to k (inclusive):
- Express
nin baseb(using the standard greedy algorithm: repeatedly divide byband collect remainders). - Sum all the digits of that base-
brepresentation.
Return the grand total — the sum of all per-base digit sums.
Notes
- Digit values in any base are always in the range
[0, b-1]; they are the remainders when you repeatedly divide byb. - When
n = 0, every base representation is just"0", so each base contributes0to the total. - When
k < 2, there are no bases to process; return0.
Constraints
0 <= n <= 10^90 <= k <= 10^6
Examples
Example 1
Input: sum_of_digits_all_bases(10, 4)
Output: 8
Example 2
Input: sum_of_digits_all_bases(0, 10)
Output: 0
Example 3
Input: sum_of_digits_all_bases(7, 2)
Output: 3
Hints
Hint 1
For each base b, you can extract digits by repeatedly taking n % b and then doing n //= b until n becomes 0 — this is the standard base-conversion loop.
Hint 2
The digit sum for base b is just the sum of all remainders collected during that loop. No need to store the digits as a string.
Hint 3
When k < 2 or n == 0, handle as special cases: n == 0 always contributes 0 per base, and k < 2 means no bases to iterate.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
For each base b in [2, k], we extract the base-b digits of n using the standard repeated-division algorithm:
- While
n > 0: recordn % bas the least-significant remaining digit, then replacenwithn // b. - The digit sum is just the accumulated remainders.
We repeat this for every base and accumulate a grand total.
Edge Cases
n == 0: Every base representation is"0", so all digit sums are0. Return0immediately.k < 2: No bases exist in[2, k]. Return0.
Why It Is Correct
The standard greedy base-conversion algorithm is well-known to produce exactly the digits of the number in the given base (least significant first). Summing those remainders gives the digit sum for that base. Accumulating over all bases from 2 to k gives the required answer.
Complexity
Let L(b) = floor(log_b(n)) + 1 be the number of digits of n in base b. The total work is:
Sum_{b=2}^{k} L(b) = Sum_{b=2}^{k} (floor(log(n)/log(b)) + 1)
For large k relative to n, most bases b > n yield a single digit (the number itself, since n < b). Specifically:
- For
b > n, the loop body executes exactly once (one remainder =n, thenn // b = 0). - For
b <= n, the loop executesO(log_b n)times.
In the worst case (n = 10^9, k = 10^6), the dominant cost is the O(k) single-iteration passes for bases b > n plus a manageable sum for small bases. This runs comfortably within time limits.
Time complexity: O(k + n * H(n)) where H(n) is a harmonic-like sum over small bases — in practice O(k) for large k.
Space complexity: O(1) extra space.
Reference Solution
from typing import List
def sum_of_digits_all_bases(n: int, k: int) -> int:
if n == 0 or k < 2:
return 0
total = 0
for b in range(2, k + 1):
num = n
while num > 0:
total += num % b
num //= b
return total