[Algorithm] Calculate the square root 2

In a technical interview at the company XXX, a programmer "senior" responsible interview Teo Teo ask a question:

"Please write a C program calculate square roots 2 of integers x "

Teo chuckled and thought to himself, "The company's leading technology Vietnam asked a question what that easy. It is not some new guy programming!"

And Teo in a flash giving away the answer with the code shown below:


#include<math.h>
#include<stdio.h>

int main()
{
    int x;
    printf("Input x: ");
    scanf("%d", &x);
    printf("Sqrt of %d = %fn", x, sqrt(x));
}
Input x: 3
Sqrt of 3 = 1.732051

Program biên no 1 errors and correct results.

Teo confidence. Unexpected company famous XXX but ask one question so silly! it thinks.

Experienced programmer's code look Teo and praise: "You have basic!". Teo happy and thought that I was sure 100% get to work. Your right there at the other old programmer asks:
"You answer the question on, this time do not use the C library function sqrt "

Rub question seems harder. Teo started thinking. After a while he started scratching their heads. How to calculate now? Teo thought and thought ...

Time out! The interviewer commented Teo: "You still have to learn a lot". Teo sad knowing that I was slipping. However, it thinks whether it should slip out of more 1 something new, it asked the interviewer:

"How I can calculate the square root? Did I need a complex algorithm with many lines of code?".

Area developer "senior" laughed and answered: "Computers make very fast calculations, instead have absolute answers, One can catch it guess the answer to my!". Overlooking the corn hole Teo, You old engineer, laughing gently explain to.

Square root of y is defined as the number x such that: x2 == y are x = y / x. If x is the x = y results / x, otherwise the results will be 1 number x 'is between x and y / x. It is not known how many of these are, but we have 1 how to guess grab 1 Some of this is about the average!

Teo nodded.

Example: to calculate the square root 2 of 3. I guess the result is 1.0. This result is not right, Should the answer be in the range 1.0 and 3/1.0 = 2.0. The average time taken 1 as a result we have 1.5. Try again with 1.5 and 3/1.5 = 2.0 as a result we have 1.75! After several iterations we will have results Proximity to answer!

Teo sight! You laugh and engineers continue.

Because we do not have accurate results, Should the number of iterations will be infinite. However, in each iteration, I will try to see results as required precise enough yet. For example, if the answer is x = current 1.73, x2 = 2.99 and we just accuracy 2 number after comma, then 1.73 is the appropriate answer. Therefore, we will have a program to calculate the square root 2 as follows:

#include<math.h>
#include<stdio.h>

#define PRECISE 0.0001f

double mysqrt(int x)
{
    double guess = (double)x / 2;
    while (fabs(guess*guess - x)/x >= PRECISE)
        guess = (x/guess - guess) / 2 + guess;
    return guess;
}

int main(void)
{
    int x;
    printf("Input x: ");
    scanf("%d", &x);
    printf("Sqrt of %d = %fn", x, mysqrt(x));
    return 0;
}

Input x: 3
Sqrt of 3 = 1.732051

Teo and enlightened!

Source: ktmt.github.io/blog