483. Smallest Good Base

来源:互联网 发布:淘宝旺铺版本区别 编辑:程序博客网 时间:2024/05/24 02:50

http://blog.csdn.net/hanzheng992/article/details/54879692

class Solution(object):
    def smallestGoodBase(self, n):
        """
        :type n: str
        :rtype: str
        """        
        num = int(n)
        thisLen = int(math.log(num,2)) + 1
        while thisLen > 2:
            # from equation [3], we havve
            thisBase = int(num ** (1.0/(thisLen - 1)))
            # from equation [2], we have
            if num * (thisBase - 1) == thisBase ** thisLen - 1:
                return str(thisBase)
            thisLen -= 1
        return str(num - 1)