From a23baf9db11959f4d9f117fb7c7be52b33cd9416 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Fri, 24 Apr 2020 20:54:32 +0300 Subject: [PATCH 1/8] Create sqrt_double Calculates the sqrt of every number in O(logn) with fixed precision. --- math/sqrt_double | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 math/sqrt_double diff --git a/math/sqrt_double b/math/sqrt_double new file mode 100644 index 000000000..ee0c723ef --- /dev/null +++ b/math/sqrt_double @@ -0,0 +1,32 @@ +#include +#include + +/* Calculate the square root of any number in O(logn) time, with precision fixed */ + +double Sqrt(double x){ + double l = 0, r = x; + //Epsilon is the precision. A great precision is between 1e-7 and 1e-12. + double epsilon = 1e-12; + while(l <= r){ + double mid = (l + r) / 2; + if(mid * mid > x){ + r = mid; + } else { + if(x - mid * mid < epsilon){ + 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); +} From 2b917ee43a6b1bd3c8f1966084b35e9e55f02956 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:17:15 +0300 Subject: [PATCH 2/8] Rename sqrt_double to sqrt_double.cpp --- math/{sqrt_double => sqrt_double.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename math/{sqrt_double => sqrt_double.cpp} (100%) diff --git a/math/sqrt_double b/math/sqrt_double.cpp similarity index 100% rename from math/sqrt_double rename to math/sqrt_double.cpp From 878dc0bf6cef88b38842b3a62fd86416dd4ed85f Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:21:45 +0300 Subject: [PATCH 3/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index ee0c723ef..b8027258d 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -1,18 +1,29 @@ #include #include -/* Calculate the square root of any number in O(logn) time, with precision fixed */ +/* Calculate the square root of any +number in O(logn) time, +with precision fixed */ -double Sqrt(double x){ +double Sqrt(double x) +{ double l = 0, r = x; - //Epsilon is the precision. A great precision is between 1e-7 and 1e-12. + /* Epsilon is the precision. + A great precision is + between 1e-7 and 1e-12. double epsilon = 1e-12; - while(l <= r){ + */ + while( l <= r ) + { double mid = (l + r) / 2; - if(mid * mid > x){ + if( mid * mid > x ) + { r = mid; - } else { - if(x - mid * mid < epsilon){ + } + else + { + if( x - mid * mid < epsilon ) + { return mid; } l = mid; From d4ab125657f27a21c3db215ada5c99ec10a7cdff Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:25:00 +0300 Subject: [PATCH 4/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index b8027258d..6401ab2a3 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -5,39 +5,34 @@ number in O(logn) time, with precision fixed */ -double Sqrt(double x) -{ +double Sqrt(double x) { double l = 0, r = x; /* Epsilon is the precision. A great precision is between 1e-7 and 1e-12. double epsilon = 1e-12; */ - while( l <= r ) - { + while ( l <= r) { double mid = (l + r) / 2; - if( mid * mid > x ) - { + if ( mid * mid > x ) { r = mid; } - else - { - if( x - mid * mid < epsilon ) - { + else { + if ( x - mid * mid < epsilon ) { return mid; } l = mid; } } return -1; + } -int main() -{ +int main() { double n{}; std::cin >> n; assert(n >= 0); - //Change this line for a better precision + // Change this line for a better precision std::cout.precision(12); std::cout << std::fixed << Sqrt(n); } From e9a0234cbe7c297e9a1d28e916924e75daabf48d Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:26:36 +0300 Subject: [PATCH 5/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index 6401ab2a3..074eb128f 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -16,8 +16,7 @@ double Sqrt(double x) { double mid = (l + r) / 2; if ( mid * mid > x ) { r = mid; - } - else { + } else { if ( x - mid * mid < epsilon ) { return mid; } @@ -25,7 +24,6 @@ double Sqrt(double x) { } } return -1; - } int main() { From c8f11deea2928f0b5b103f7d5003ef3207110146 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:28:31 +0300 Subject: [PATCH 6/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index 074eb128f..2b13b2006 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -12,8 +12,8 @@ double Sqrt(double x) { between 1e-7 and 1e-12. double epsilon = 1e-12; */ - while ( l <= r) { - double mid = (l + r) / 2; + while ( l <= r ) { + double mid = ( l + r ) / 2; if ( mid * mid > x ) { r = mid; } else { @@ -25,7 +25,6 @@ double Sqrt(double x) { } return -1; } - int main() { double n{}; std::cin >> n; From f27f3fcbf007055e27db2a6493b17c2b46583af3 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:29:50 +0300 Subject: [PATCH 7/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index 2b13b2006..6d7641dcb 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -13,7 +13,7 @@ double Sqrt(double x) { double epsilon = 1e-12; */ while ( l <= r ) { - double mid = ( l + r ) / 2; + double mid = (l + r) / 2; if ( mid * mid > x ) { r = mid; } else { From a4d3475ed520567b5d80810ff99144e8bf290292 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:30:52 +0300 Subject: [PATCH 8/8] Update sqrt_double.cpp --- math/sqrt_double.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index 6d7641dcb..e3418bfc3 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -12,6 +12,7 @@ double Sqrt(double x) { between 1e-7 and 1e-12. double epsilon = 1e-12; */ + double epsilon = 1e-12; while ( l <= r ) { double mid = (l + r) / 2; if ( mid * mid > x ) {