Hong-Phuc Bui
2024-06-17 689ada49c2f083fcb3a2eeafc2ac15954ed64eca
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}")