Square Roots

Problem #9

Tags: computing instructional

Who solved this?

Previous:Compound Interest II Next:Standard Deviation


Next we will look at a basic mathematical operation which you may not have learned how to calculate by hand, square roots. Intuitively the concept of a square root is easy to understand: if a * a = b, then b is the square of a, and likewise a is the square root of b. For example, the square root of 9 is 3, because 3 * 3 = 9.

However it's not immediately clear how we would actually calculate the answer for any given value, for example how would we determine that the square root of 13689 is 117, or that the square root of 2 is approximately 1.414214...? After the advent of personal calculators, the method for this calculation has become somewhat esoteric knowledge, but here is the method, finding the square root of 5386.3928 as an example:

1. First separate the given number's digits into pairs, starting at the decimal point. Place the number under a radical sign.

 ___________
√53 86.39 28

2. Starting from the leftmost pair, determine the greatest digit whose square is less than that pair, and write it above as the first digit of our final answer.

  7
 ___________
√53 86.39 28

3. We then take the difference of that chosen digit squared from the first pair and write it below. Then we drop down the next pair of digits.

        7
       ___________
  7   √53 86.39 28
     - 49
     ----
        4 86

4. Then we double the answer so far, write it down to the left, and leave space for one more digit.

        7
       ___________
  7   √53 86.39 28
     - 49
     ----
14_     4 86

5. To determine the missing digit we ask ourselves, which number would fill in the blank and also multiply to that number to get as close as possible to the number on the right without going over? For example, 1 * 141 = 141, 2 * 142 = 284, 3 * 143 = 429, 4 * 144 = 576, so the next digit is 3.

        7  3
       ___________
  7   √53 86.39 28
     - 49
     ----
143     4 86

6. We then repeat the process from step 3 until either it terminates with a difference of 0 indicating an exact value, or we compute some desired quantity of digits.

               7  3. 3  9  2  0  4  8
              _______________________
         7   √53 86.39 28 00 00 00 00
         ^  - 49
            ----
       143     4 86
         ^   - 4 29
                ------
      1463       57 39
         ^     - 43 89
                 -------
     14669       13 50 28
         ^     - 13 20 21
               ----------
    146782          30 07 00
         ^        - 29 35 64
                  ----------
   1467840             71 36 00
         ^           -        0
                     ----------
  14678404             71 36 00 00
         ^           - 58 71 36 16
                     -------------
 146784088             12 64 63 84 00
         ^           - 11 74 27 27 04
                     ----------------
146784096_                90 36 56 96

Ando so the square root of 5386.3928 is approximately 73.392048....
Note that the calculation could continue, if we wanted to compute more than 6 digits.
Also note that as the next digit would be 6, the answer rounded to 6 decimal places would actually be 73.392049.

Problem Statement

You will be given several numbers. Return the square root of each.

Input Data
First line will be Q, the quantity of testcases.
Q lines will then follow, each with a single value b.

Answer
Should consist of Q values, being the square root of each testcase value.
Round each answer to 50 digits after the decimal point.
Remove all trailing zeroes.

Example

input data:
3
13689
2
5386.3928

answer:
117 1.41421356237309504880168872420969807856967187537695 73.3920486156368304437511608966954008189135090722943
You need to login to get test data and submit solution.