Skip to content

Commit 7c9ba2b

Browse files
authored
Merge pull request vJechsmayr#538 from livace/483
Add 483. Smallest Good Base
2 parents 746ae9e + 97d2729 commit 7c9ba2b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import math
2+
3+
class Solution(object):
4+
def smallestGoodBase(self, n):
5+
"""
6+
:type n: str
7+
:rtype: str
8+
"""
9+
n = int(n)
10+
11+
for length in range(64, 2, -1):
12+
l = 2
13+
r = int(math.sqrt(n) + 1)
14+
15+
def sum(mid):
16+
res = 0
17+
for i in range(length - 1, -1, -1):
18+
res += mid ** i
19+
if res > n:
20+
return res
21+
return res
22+
23+
while l < r - 1:
24+
mid = (l + r) // 2
25+
if sum(mid) <= n:
26+
l = mid
27+
else:
28+
r = mid
29+
30+
if sum(l) == n:
31+
return str(l)
32+
33+
return str(n - 1)

0 commit comments

Comments
 (0)