Kaushik Narayan R 9da326288a day 2: 69-sqrt-x
also refactored, added drivers
2023-09-27 11:22:43 -07:00

32 lines
488 B
C++

#include "soln.hpp"
#include <cmath>
int Solution::mySqrt(int x)
{
// base case
if (x == 0 || x == 1)
{
return x;
}
// effectively only till ~sqrt(x)
for (int i = 2; i <= x; i++)
{
long i_square = long(i) * i;
// x is a perfect square
if (i_square == x)
{
return int(i);
}
// x is not a perfect square
else if (i_square > x)
{
return int(i - 1);
}
}
return -1; // compiler fuss
}
bool Solution::test(int x, int answer)
{
return answer == mySqrt(x);
}