Junior — Middle
How to find the square root of a number using only addition, subtraction, multiplication, and division operations?
sobes.tech AI
Answer from AI
To calculate the square root of a number without using built-in functions, you can apply Newton's method (the tangent method), using only addition, subtraction, multiplication, and division operations.
Method idea:
-
Start with an initial approximation x (for example, x = number / 2).
-
Iteratively improve the approximation using the formula:
x = (x + number / x) / 2
-
Repeat until the difference between the current and previous values becomes sufficiently small.
Example in Python:
number = 25.0
x = number / 2.0
epsilon = 1e-10
while True:
prev_x = x
x = (x + number / x) / 2.0
if abs(x - prev_x) < epsilon:
break
print(f"Square root of {number} ≈ {x}")
This method converges quickly and does not require special functions.