a=1/2+3/4+7/8+15/16+……+511/512
=9-(1/2+1/4+……+1/128+1/256+ 1/512+ 1/512- 1/512)
=9-[1/2+1/4+……+1/128+1/256+ (1/512+ 1/512)- 1/512]
=9-[1/2+1/4+……+1/128+1/256+ (2/512)- 1/512]
=9-{1/2+1/4+……+1/128+[1/256+ (1/256)]- 1/512}
=9-[1/2+1/4+……1/128+1/128-1/512] (这里的规律就是分出一个1/512后 无限与前面合并)
……
已知 2^9=512 所以 一共需要9个1来减
=9-(1/2+1/4+1/4-1/512)
=9-(1/2+1/2-1/512)
=9-511/512
=4097/512
代码验证答案如下
↑ 这是算出来的答案
a=1/2+3/4+7/8+15/16+……+511/512
=1-1/2+1-1/4+……+1-1/512
=9-(1/2+1/2^2+1/2^3+……+1/2^9)
=9-(1/2x(1-1/2^9))/(1-1/2)
=9-511/512
=4097/512