Smallest base with all digits equal
For a number M, find the smallest base B ≥ 2 in which every digit of M is the same. For example, 63 is 333 in base 4.
Reported in: Infosys (SP / DSE · earlier papers) (memory-based)
Input
One line: M.
Output
The smallest such base.
Constraints
1 ≤ M ≤ 10^12.
Example 1
Input:
63
Output:
2Example 2
Input:
41
Output:
40Example 3
Input:
7
Output:
2Example 4
Input:
2
Output:
3Hint: If M has three or more digits in base B then B² ≤ M, so only about √M bases need checking one by one. Beyond that M has two equal digits, M = d·(B + 1) with d < B, so try the divisors d of M; if nothing works, base M + 1 makes M a single digit.