Saturday, March 31, 2012

Find square root of a number without using square root

Give a number find square root of number without using square root function.

Solution:
So given x we need to find sqrroot(x) = y
i.e. x = y^2

so we need to a find a y whose square is x.

We can use binary search to find such a y

Fo eg we need to find sqrroot(100)
We can start doing binary search between 0 and 100. First iteration we find 50 and 50^2 > 200 so we move left partition and visit 0 and 50 and keep continuing till we find our number

No comments:

Post a Comment