Problem C
Babylonian Square Root Algorithm
This exercise is based on chapter
Let us implement the Babylonian square root algorithm. Here is a pseudocode description of the algorithm:
-
Let
be the number for which to find the square root. -
Let
be the initial guess. -
Let
be the tolerance. -
Let
be our previous guess, initialized to . -
While the absolute difference between
and is greater than :-
. -
, the average of and .
-
Input
Input consists of three lines and they are as follows:
-
- an integer for which to find the square root, where . -
- an integer representing from where to start the search for the actual square root, where . -
- a floating point number, indicating the minimum change between iterations until the algorithm stops, where . The number is given with at most digits after the decimal point.
Output
Output consists of two lines. The first line contains the
square root of
Sample Input 1 | Sample Output 1 |
---|---|
2 1 0.0001 |
1.4142 4 |
Sample Input 2 | Sample Output 2 |
---|---|
9 2 0.00001 |
3.0000 5 |
Sample Input 3 | Sample Output 3 |
---|---|
1 1 0.00001 |
1.0000 1 |