A more programming-like solution, is to hack the problem from simple: we try each possble base value, and see which 111..11 fits target number - using binary search.
class Solution(object): def helper(self, num, p): l = 1; r = int(pow(num, 1.0/p) + 1) # find highest possible bit while(l < r): mid = l + (r - l) // 2 sum = 0; cur = 1 # Addup to sum by form of 1111..11 for i in xrange(0, p + 1): sum += cur cur *= mid # Good? if sum == num: return mid elif sum > num: r = mid else: l = mid + 1 return -1 def smallestGoodBase(self, N): n = int(N) # iterate each base, from longest 1s to shortest for p in xrange(2, 101): if (1 << p) < n: k = self.helper(n, p) if k != -1: return str(int(k)) return str(n-1) # 11
However, as you expect, there's a smarter, math solution.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步