假设我的方程T = sum(A ** n),其中n从1到M。
现在让我说我认识M和T,但是想要A。我该如何求解A?
如果发生错误,我想进行指数补偿,但是我不希望花费的总补偿时间大于T,也不希望重试的最大次数超过M。所以我需要找到A。
答案 0 :(得分:2)
n从1到M的sum(A ** n)的闭式解为(A ^(M + 1)-1)/(A-1)-1。 = 3且A =2。则2 ^ 1 + 2 ^ 2 + 2 ^ 3 = 14,而(2 ^ 4-1-/(2-1)-1 = 15 / 1-1 = 14。
因此,我们有一个封闭形式的表达式T =(A ^(M + 1)-1)/(A-1)-1。这是一个超越方程,没有封闭形式的解决方案。但是,由于RHS在A中单调增加(A的值越大,表达式的值就越大),那么我们可以做等于二分查找的方法来找到任意精度的答案:
L = 0
H = MAX(T, 2)
A = (L + H) / 2
while |(A ^ (M + 1) - 1) / (A - 1) - 1 - T| > precision
if (A ^ (M + 1) - 1) / (A - 1) - 1 > T then
H = A
else then
L = A
end if
A = (L + H) / 2
loop
例如:T = 14,M = 3,ε= 0.25
L = 0
H = MAX(15, 2) = 14
A = L + H / 2 = 7
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 385 > 0.25
H = A = 7
A = (L + H) / 2 = 3.5
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 44.625 > 0.25
H = A = 3.5
A = (L + H) / 2 = 1.75
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 3.828125 > 0.25
L = A = 1.75
A = (L + H) / 2 = 2.625
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 13.603515625 > 0.25
H = A = 2.625
A = (L + H) / 2 = 2.1875
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 3.440185546875 > 0.25
H = A = 2.1875
A = (L + H) / 2 = 1.96875
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.524444580078125 > 0.25
L = A = 1.96875
A = (L + H) / 2 = 2.078125
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 1.371326446533203125 > 0.25
H = A = 2.078125
A = (L + H) / 2 = 2.0234375
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.402295589447021484375 > 0.25
H = A = 2.0234375
A = (L + H) / 2 = 1.99609375
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.066299498081207275390625 < 0.25
Solution: 1.99609375