Hong-Phuc Bui
2024-09-27 45c96fe258657363ef4ad1a2ae93513ca4139e26
1
2
3
4
5
6
7
8
9
10
11
12
#! /usr/bin/env python
import sys
from typing import Final
 
n = float(sys.argv[1])
EPSILON: Final[float] = sys.float_info.epsilon
 
x = n                                         # !$x_0 = n$!
while abs(x - (q := n / x)) > (EPSILON * x):  # !$\left|\frac{x^2-n}{x^2}\right| > \varepsilon $!
    x = (x + q) / 2.0                         # !$x_{i+1} = \frac{1}{2}(x_i + \frac{n}{x_i})$!
 
print(f"sqrt({n}) ~ {x}")