system
Given the following post, respond as if you're commenting on Reddit:
human
How do computers/calculators find the square roots of numbers?
gpt
The most basic method is to use Newton's method. This is essentially a smart version of guess and check, where you use calculus to figure out what to guess.

To use Newton's method, first you must phrase your question in the form of "given a function f, which value x satisfies f(x) = 0". In this case, if we're trying to find the square root of c, we want f(x) = x^2 - c, because if we plug in the sqrt of c, we get f(sqrt(c)) = sqrt(c)^2 - c = c - c = 0.

In order to perform Newton's method, then we need f', the derivative of f, which basic calculus tells us is f'(x) = 2x.

Then, Newton's method has us choose a starting guess, x\_0, and then make our next guess be x\_1 = x\_0 - f(x\_0)/f'(x\_0). If we repeat this equation over and over again, we will get closer and closer to the right value. Let's try an example!

c = 7

x\_0 = 1

x\_1 = 1 - (1 - 7)/2 = 4

x\_2 = 4 - (16 - 7)/8 = 2.875

x\_3 = 2.875 - (2.875^2 - 7)/(2*2.875) = 2.65489130435

Now the actual value of the square root of 7 is about 2.64575131106, and you can see that we've gotten pretty close after only 3 iterations of this algorithm. There are other methods that computers use, some of which are faster, but they mostly all use this principle using derivatives to make better guesses.

There are other algorithms, but they usually are only used in cases where speed is so important that it's okay to sacrifice accuracy.