Goldman Sachs Interview Question
Software Engineer / Developersbest way :
double a = (double) num;
double x = 1;
// For loop to get the square root value of the entered number.
for( int i = 0; i < n; i++)
{
x = 0.5 * ( x+a / x );
}
System.out.println("sqrt of " + a + " is " + x);
Using binary search actually is the worst way to do that. Because when the square just get to 10 the x = 100 and the time used to find 100's square is log2(100) almost equal to 7(becz 2^7 = 128) just a little bit overruns the linear search.
My way is to check linearly but a little bit different:
int Find_SR(int x)
{
vector<int> T;
int rest = x; /*to store the rest factor*/
for(int i=2;i<n;)
{
if(rest%(i*i)==0)
{
rest = rest/(i*i);
T.push_back(i);
i=2;
}
else
{
i++;
}
}
// using for loop to print out the factor that stored in vector.
}
How about one of the following approaches:
- Vijay October 28, 20101) find exp (0.8 * log x)
2) Using Newton-Raphson/Bisection method to solve the equation: y^2 - x = 0.