Facebook Interview Question
Software Engineer / Developersdouble sqroot(int i)
{
if(i<0) {
return -1;
}
double t = 1.0;
double tsq, ival;
tsq = t*t;
ival = i*l;
while(1) {
tsq = t*t;
ival = i*1.0;
if(fabs(tsq-ival)<EPS) {
break;
}
t+=0.001;
if(t>=i)
break;
}
return t;
}
A simple way to compute the sqrt is by adding the odd numbers till it becomes greater than that number.
so, 1 + 3 <= 4. So, we count the number of times we do this operation.
likewise, sqrt (9) would be 1 + 3 + 5. So, it would be 3.
Here's how this looks.
int
sqrt(unsigned int n)
{
int count = 0;
int cur_term = 1;
int next_term = 1;
if (n < 2) {
return (n);
}
while (cur_term <= n) {
cur_term += next_term;
next_term += 2;
count++;
}
return (count);
}
LOL at you being awed by that.
1) That solution is O(sqrt(n)), and how different is it from just computing consecutive squares and then comparing?
2) If all we are looking for the the integer part of the square root, just do a binary search, which is O(logn).
3) The solution (Newton's method) given before also computes the non-integral part, and converges really fast (O (log n)) and can be generalized to cube roots, k^th roots etc.
Lot of silly posts on this site.
The take away is to use the strategy of sum of odd number is a square number.
We can easily extend this strategy to right shift the number by two places, and performing the same operation till we achieve recurring / temination decimal number.
pity on you guys :-(
so many crappy posts...we should ask admin people to block these crappy posters once n for all...
/* C implementation of http://en.wikipedia.org /wiki/Methods_of_computing_square_roots#Rough_estimation
*/
#include <stdio.h>
#include <math.h>
int
rough_estimate(int r)
{
int i = 1,d = 0,n,val;
while (r > i) {
i += i*10;
d++;
}
if ( (d % 2) == 0) {
/* d is even */
n = (d - 2)/2;
val = 6*pow((float)10,(float)n);
} else {
/* d is odd */
n = (d - 1)/2;
val = 2*pow((float)10,(float)n);
}
return val;
}
main()
{
int v,j;
int arg=30000;
float v1;
v = rough_estimate(arg);
v1 = (float)v;
for (j = 0; j < 10; j++) {
v1 = (v1 + arg/v1)/2;
}
printf("Square root of %d = %f\n",arg,v1);
printf("Square root of %d = %f\n",arg,sqrt(arg));
}
java implementation.
public class SquareRoot {
public SquareRoot() {}
public double computeSquareRoot(int value) {
double start = 0.0;
double end = value;
double middle = (start+end)/2;
double minError = 0.00001;
double error = middle*middle-value;
while(Math.abs(error) > minError) {
if(error < 0) start = middle;
else if(error > 0) end = middle;
middle = (start+end)/2;
error = middle*middle-value;
}
return middle;
}
public static void main(String[] args) {
SquareRoot sr = new SquareRoot();
double root = sr.computeSquareRoot(12);
System.out.println(root);
}
}
C# version:
static double Sqrt(double value)
{
const double Threshold = 0.0000001;
if (value < 0)
{
throw new ArgumentOutOfRangeException("value");
}
double min = 0, max = value;
while (true)
{
double mid = (min + max) / 2;
double diff = mid * mid - value;
if (Math.Abs(diff) <= Threshold)
return mid;
if (diff > 0)
max = mid;
else
min = mid;
}
}
double squareroot(double num)
- Anonymous April 20, 2009{
double number;
number =num/2;
do
{
number=(number+num/nuber)/2
}
while (number*number-num >0.00001)
return number;
}
}