You are on page 1of 4

Bài

W02H03 – Chủ đề: Căn bậc hai


Cánh cụt Mathuinin cần giúp đỡ của bạn. Ở trường học, các chú chim cánh cụt dạy về căn bậc hai.
Mathuinin đã nắm được kiến thức cơ bản nhưng vẫn có tính sai thường xuyên. Để có thể kiểm tra các
bước trung gian của Mathuinin, cô ấy yêu cầu bạn viết một chương trình.

Code thầy cho trước:

Đề bài:

Implement the method sqrt. This receives a double n, whose square root is to be calculated. For this,
the method of manually extracting square roots will be used, which is explained below. In addition,
certain intermidate steps should be output to the console. You can also find more information on this
below. Following are some restrictions that have to be followed:

• |n| <= Integer.MAX_VALUE = 2,147,483,647


• Only the first 4 decimal places of n are relevant for the calculation. All others can be cut off
(except in the 1st output line)
• The method should calculate the exact result to 2 decimal places. This means that it is not
rounded, but “truncated” before the 3rd decimal place (e.g. 1.347 become 1.34)
• Functions from java.lang.Math are not allowed

Square root extractions can be calculated (manually) as follows:

1. The number must be divided into groups of digits of size 2, starting from the decimal point
(i.e. to the left for the integer part, to the right for the decimal places). If there is an odd
number of digits, a 0 is used for padding. 1.234 becomes e.g. 01 | 23 | 40 divided, not 12 | 34)
2. Start with the first group of digits as the minuend, with 1 as the subtrahend. As long as the
result is >= 0, calculate the difference and increase the subtrahend by 2
3. The number of distinct subtrahends subtracted in step 2 is the first digit of the result
4. The remaining digits of the result can be computed similarly: take the remainder from the
previous step and append the next group of digits (if all groups of digits have already been
processed, append 00). This is the new minuend. Calculate the new starting subtrahend by
appending 1 to double the current result. As long as the result is >= 0, compute the difference
and increase the subtrahend by 2. The number of subtrahends subtracted is the next digit of
the result.
5. Repeat step 4 until all digits of the input number have been processed and the remainder is 0
after one step or the requested accuracy has been reached. In the result, the decimal point
comes before the digit that was calculated using the first group of digits after the decimal
point of the input number.

Ruth also prepared an example for you (see image below):

In this example, we want to calculate the square root of 1049.76. To do this, the number is first
divided into groups (1.): 10 | 49 | 76. In the next step (2.), first 1 (intermediate result: 9), then 3
(intermediate result: 6) and finally 5 (intermediate result: 1) are subtracted from the 10. 7 can no
longer be subtracted, otherwise the result would be negative. Therefore, since 3 numbers were
subtracted, 3 is also the 1st digit of the result (3rd). Now you can continue to calculate accordingly:
First, the next group of digits (49) is appended to the rest of the last step (1). The next minuend is 149.
The first subtrahend results from the double of the previous result (2*3 = 6) and an appended 1, i.e.
61. From 149 -61 -63 = 25 follows a 2 as the next digit of the result (currently 32) and remainder 25 for
the next step. Now that the decimal point has been reached, it must also be inserted into the result
(32nd). Now the last group of digits is missing: Add a new group of digits to the last remainder ->
Minuend 2576. Add 1 to double the previous result for the new subtrahend: 2 * 32 = 64 =>
Subtrahend 641. From 2576 -641 -643 -645 - 647 = 0 we get the new result digit 4. We also have
remainder 0 and no remaining digit sequence. This completes the calculation and the result 32.4 is
exact.


To verify her calculations, Ruth wants your program to produce the following output:

1. 1st line: "root⎵of⎵<number whose root is calculated (with all passed decimal places, . as
separator)>"
2. 2nd line empty
3. For each group of digits, the following block, where x is the number of subtrahends:

"<current minuend>

--------

-<1. subtrahend>

-<…>

-<x. subtrahend>

--------

remainder:⎵<current remainder>

new⎵result⎵digit:⎵<new result digit = x>

<blank line>"

4. As the last line: "Result:⎵<the result>"


5. Only one line should be output for a negative number, namely: "No⎵negative⎵roots!"

So, for the example above, the output would be:

Another example of an output: square root of 4


Ruth also has a few notes to make your task easier:

The output of the number in the 1st line should correspond to the number in the standard java string
representation, e.g. "1.0E9" for one billion, "2.0" for 2 and "0.1" for 0.1. The same applies to the
output of the final result.

Only subtrahends that can also be successfully subtracted from the minute end should be output. If
there is none, then the two "--------" lines follow each other directly.

Blocks of 0 are not relevant if they are used to calculate the first or last digits of the result. However,
at least 1 block must always be issued. So the output for square root of 0.0004 would be the same as
for square root of 4 in the above example (except for the 1st and last line, of course). However, the 0
blocks can also be output, as in the next example.

"." is used to describe a decimal separator (see sample output)

You might also like