2020-04-25 01:54:32 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <cassert>
|
|
|
|
|
2020-04-25 21:21:45 +08:00
|
|
|
/* Calculate the square root of any
|
|
|
|
number in O(logn) time,
|
|
|
|
with precision fixed */
|
2020-04-25 01:54:32 +08:00
|
|
|
|
2020-04-25 21:21:45 +08:00
|
|
|
double Sqrt(double x)
|
|
|
|
{
|
2020-04-25 01:54:32 +08:00
|
|
|
double l = 0, r = x;
|
2020-04-25 21:21:45 +08:00
|
|
|
/* Epsilon is the precision.
|
|
|
|
A great precision is
|
|
|
|
between 1e-7 and 1e-12.
|
2020-04-25 01:54:32 +08:00
|
|
|
double epsilon = 1e-12;
|
2020-04-25 21:21:45 +08:00
|
|
|
*/
|
|
|
|
while( l <= r )
|
|
|
|
{
|
2020-04-25 01:54:32 +08:00
|
|
|
double mid = (l + r) / 2;
|
2020-04-25 21:21:45 +08:00
|
|
|
if( mid * mid > x )
|
|
|
|
{
|
2020-04-25 01:54:32 +08:00
|
|
|
r = mid;
|
2020-04-25 21:21:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( x - mid * mid < epsilon )
|
|
|
|
{
|
2020-04-25 01:54:32 +08:00
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
l = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
double n{};
|
|
|
|
std::cin >> n;
|
|
|
|
assert(n >= 0);
|
|
|
|
//Change this line for a better precision
|
|
|
|
std::cout.precision(12);
|
|
|
|
std::cout << std::fixed << Sqrt(n);
|
|
|
|
}
|