Don't use sqrt because precision issues↵
↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
↵
Also here is implementation to find floor(sqrt(x))↵
↵
~~~~~↵
#define int long long↵
int sqrt(int x){↵
int l=1,r=1e9+5,ans=0,mid;↵
while (l<=r){↵
mid=(l+r)/2;↵
if (mid*mid<=x){↵
ans=mid;↵
l=mid+1;↵
}else r=mid-1;↵
}return ans;↵
}↵
~~~~~↵
It seems std::sqrt works fine in C++17 though. Also sqrtl seems to give correct results↵
↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
implement sqrt using binary search↵
↵
↵
~~~~~↵
#define int long long↵
int sqrt(int x){↵
int l=1,r=1e9+5,ans=0,mid;↵
while (l<=r){↵
mid=(l+r)/2;↵
if (mid*mid<=x){↵
ans=mid;↵
l=mid+1;↵
}else r=mid-1;↵
}return ans;↵
}↵
~~~~~↵
It seems std::sqrt works fine in C++17 though. Also sqrtl seems to give correct results↵