def bisection_recursive(lo, hi, a):
if (hi - lo <= 0.00001):
return (lo + hi) / 2
else:
m = (lo + hi) / 2
if( f(lo, a) * f(m, a) < 0 ):
return bisection_recursive(lo, m, a)
else:
return bisection_recursive(m, hi, a)
def f(x, a):
return x * x - a
def bisection(lo, hi, a):
EPS = 0.00001
while not hi - lo <= EPS:
m = (lo + hi) / 2
print(f'[{lo}, {hi}]', lo, hi)
if f(lo, a) * f(m, a) < 0:
hi = m
else:
lo = m
return m
def bisection2(lo, hi, a):
EPS = 0.00001
while not hi - lo <= EPS:
m = (lo + hi) / 2
x = lo * lo - a
y = m * m - a
print(f'[{lo}, {hi}]')
print()
if (x > 0 and y < 0) or (x < 0 and y > 0):
hi = m
else:
lo = m
return hi
def main():
n = int(input())
res = bisection2(0, n + 5, n)
res_rec = bisection_recursive(0, n + 5, n)
print(res)
print()
print(res_rec)
main()
ZGVmIGJpc2VjdGlvbl9yZWN1cnNpdmUobG8sIGhpLCBhKToKCiAgICBpZiAoaGkgLSBsbyA8PSAwLjAwMDAxKToKCiAgICAgICAgcmV0dXJuIChsbyArIGhpKSAvIDIKCiAgICBlbHNlOgoKICAgICAgICBtID0gKGxvICsgaGkpIC8gMgoKICAgICAgICBpZiggZihsbywgYSkgKiBmKG0sIGEpIDwgMCApOgogICAgICAgICAgICByZXR1cm4gYmlzZWN0aW9uX3JlY3Vyc2l2ZShsbywgbSwgYSkKICAgICAgICBlbHNlOgogICAgICAgICAgICByZXR1cm4gYmlzZWN0aW9uX3JlY3Vyc2l2ZShtLCBoaSwgYSkKCmRlZiBmKHgsIGEpOgoKICAgIHJldHVybiB4ICogeCAtIGEKCmRlZiBiaXNlY3Rpb24obG8sIGhpLCBhKToKCiAgICBFUFMgPSAwLjAwMDAxCgogICAgd2hpbGUgbm90IGhpIC0gbG8gPD0gRVBTOgoKICAgICAgICBtID0gKGxvICsgaGkpIC8gMgoKICAgICAgICBwcmludChmJ1t7bG99LCB7aGl9XScsIGxvLCBoaSkKCiAgICAgICAgaWYgZihsbywgYSkgKiBmKG0sIGEpIDwgMDoKCiAgICAgICAgICAgIGhpID0gbQoKICAgICAgICBlbHNlOgoKICAgICAgICAgICAgbG8gPSBtCgogICAgcmV0dXJuIG0KCmRlZiBiaXNlY3Rpb24yKGxvLCBoaSwgYSk6CgogICAgRVBTID0gMC4wMDAwMQoKICAgIHdoaWxlIG5vdCBoaSAtIGxvIDw9IEVQUzoKCiAgICAgICAgbSA9IChsbyArIGhpKSAvIDIKCiAgICAgICAgeCA9IGxvICogbG8gLSBhCiAgICAgICAgeSA9IG0gKiBtIC0gYQoKICAgICAgICBwcmludChmJ1t7bG99LCB7aGl9XScpCgogICAgICAgIHByaW50KCkKCiAgICAgICAgaWYgKHggPiAwIGFuZCB5IDwgMCkgb3IgKHggPCAwIGFuZCB5ID4gMCk6CgogICAgICAgICAgICBoaSA9IG0KCiAgICAgICAgZWxzZToKCiAgICAgICAgICAgIGxvID0gbQoKICAgIHJldHVybiBoaQoKZGVmIG1haW4oKToKCiAgICBuID0gaW50KGlucHV0KCkpCgogICAgcmVzID0gYmlzZWN0aW9uMigwLCBuICsgNSwgbikKCiAgICByZXNfcmVjID0gYmlzZWN0aW9uX3JlY3Vyc2l2ZSgwLCBuICsgNSwgbikKCiAgICBwcmludChyZXMpCiAgICBwcmludCgpCiAgICBwcmludChyZXNfcmVjKQptYWluKCkK
[0, 14]
[0, 7.0]
[0, 3.5]
[1.75, 3.5]
[2.625, 3.5]
[2.625, 3.0625]
[2.84375, 3.0625]
[2.953125, 3.0625]
[2.953125, 3.0078125]
[2.98046875, 3.0078125]
[2.994140625, 3.0078125]
[2.994140625, 3.0009765625]
[2.99755859375, 3.0009765625]
[2.999267578125, 3.0009765625]
[2.999267578125, 3.0001220703125]
[2.99969482421875, 3.0001220703125]
[2.999908447265625, 3.0001220703125]
[2.999908447265625, 3.0000152587890625]
[2.9999618530273438, 3.0000152587890625]
[2.999988555908203, 3.0000152587890625]
[2.999988555908203, 3.000001907348633]
3.000001907348633
2.9999985694885254