Balanced-Radix Digital Root Collision Finder
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests mastery of base conversion and number theory by finding all integers in a range whose digital roots are identical across two different bases.
Balanced-Radix Digital Root Collision
The digital root of a positive integer n in base b is computed by repeatedly summing the digits of n (written in base b) until a single digit remains. Formally:
- Write
nin baseb. - Sum all its digits to get
s. - If
s >= b, repeat withs. - The process terminates when the result is a single base-
bdigit (i.e., a value in[1, b-1]).
For example, n = 255, b = 16:
255in base 16 isFF, digit sum =15 + 15 = 30.30in base 16 is1E, digit sum =1 + 14 = 15.15is a single hex digit → digital root = 15.
There is a well-known closed-form: for n > 0 and base b, the digital root equals 1 + ((n - 1) % (b - 1)).
Task
Given integers lo, hi, b1, and b2 (with b1 != b2), return a sorted list of all integers n in the closed interval [lo, hi] such that:
digital_root(n, b1) == digital_root(n, b2)
These are called collision integers for the two bases.
The returned list must be sorted in ascending order.
Constraints
1 <= lo <= hi <= 20002 <= b1 < b2 <= 36b1 != b2- The answer may be empty.
- You may assume
hi - lo <= 2000.
Examples
Example 1
Input: find_digital_root_collisions(1, 20, 2, 3)
Output: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Example 2
Input: find_digital_root_collisions(1, 50, 10, 16)
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 46, 47, 48, 49, 50]
Example 3
Input: find_digital_root_collisions(100, 130, 3, 5)
Output: [101, 102, 105, 106, 109, 110, 113, 114, 117, 118, 121, 122, 125, 126, 129, 130]
Hints
Hint 1
The digital root of n in base b (for n > 0) has a one-line closed form using modular arithmetic — no need to repeatedly convert and sum digits.
Hint 2
The collision condition dr(n, b1) == dr(n, b2) becomes an equation mod (b1-1) and mod (b2-1). Think about what values of n satisfy both simultaneously using the Chinese Remainder Theorem or direct enumeration.
Hint 3
Once you derive which residues mod lcm(b1-1, b2-1) produce collisions, you can iterate over the range [lo, hi] very efficiently — but a direct O(hi - lo) scan with the closed-form formula is also fast enough.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Closed-Form Digital Root
Computing digital roots by iterated digit-summing is slow and tricky to implement correctly. The elegant number-theoretic fact is:
For any integer
n >= 1and baseb >= 2:digital_root(n, b) = 1 + ((n - 1) % (b - 1))
This is a direct consequence of the fact that n ≡ (sum of base-b digits of n) (mod b-1) — exactly analogous to the familiar "casting out nines" rule in base 10.
Collision Condition
We want:
dr(n, b1) == dr(n, b2)
⟺ 1 + ((n-1) % (b1-1)) == 1 + ((n-1) % (b2-1))
⟺ (n-1) % (b1-1) == (n-1) % (b2-1)
Let m = n - 1, p = b1 - 1, q = b2 - 1. The condition becomes:
m % p == m % q
Why This Is Interesting (Deeper Analysis)
Let g = gcd(p, q) and L = lcm(p, q). Writing m = k·p + r (where 0 ≤ r < p), the condition m % p == m % q holds iff r < p (trivially) and m ≡ r (mod q). By CRT, valid residues mod L are exactly those r with 0 ≤ r < p and r ≡ 0 (mod g) — there are p/g = (b1-1)/gcd(b1-1, b2-1) of them per period of length L. This means the collision integers have a periodic structure.
Implementation
For hi ≤ 2000, a direct O(hi - lo + 1) linear scan using the closed-form formula is perfectly efficient and straightforward:
from typing import List
def find_digital_root_collisions(lo: int, hi: int, b1: int, b2: int) -> List[int]:
result = []
p = b1 - 1 # modulus for base b1
q = b2 - 1 # modulus for base b2
for n in range(lo, hi + 1):
m = n - 1
dr1 = 1 + (m % p)
dr2 = 1 + (m % q)
if dr1 == dr2:
result.append(n)
return result
Correctness
- The closed-form
1 + ((n-1) % (b-1))is valid for alln ≥ 1,b ≥ 2. - The loop enumerates every candidate exactly once and checks equality directly.
- Results are collected in ascending order because we iterate
lotohi.
Complexity
- Time: O(hi − lo + 1) — linear in the range size, at most O(2000).
- Space: O(k) where
kis the number of collision integers (output size).