From 20d44dfd5e0f7671a3270bad341befed70ceadde Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Fri, 29 Jul 2022 08:49:38 +0530 Subject: [PATCH] Add solution to "69. Sqrt(x)" in C++ --- Easy/sqrtx/solution.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Easy/sqrtx/solution.cpp diff --git a/Easy/sqrtx/solution.cpp b/Easy/sqrtx/solution.cpp new file mode 100644 index 0000000..8828ccf --- /dev/null +++ b/Easy/sqrtx/solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int mySqrt(int x) { + if (x == 0) { + return 0; + } + + int times = 20; + double xi = x; + for (int i = 0; i < times; ++i) { + xi = (xi + x / xi) / 2; + } + + return (int)xi; + } +};