排列硬币
# 排列硬币
# 暴力求解
def func1(n: int):
for i in range(1, n + 1):
n -= i
if n < i:
return i
# 二分求解
def func2(n: int):
low = 1
high = n
while low <= high:
mid = (low + high) // 2
if (mid + 1) * mid / 2 == n:
return mid
elif (mid + 1) * mid / 2 < n:
low = mid + 1
else:
high = mid - 1
# 牛顿迭代求解 (i + n/i)/2 <= n
def func3(x: int):
return func(1, x)
def func(x: int, n: int):
res = (x + (2*n-x)/x) / 2
if res == x:
return x
else:
return func(res, n)
print(func1(10))
print(func2(10))
print(func3(10))