Binary search isn't the right term.  Rather you want "bisection."  But
for each iteration it adds only one bit to the precision of the
answer.

You should also look at Newton's method. It doubles the number of bits
of precision in each iteration.

Algorithm: float sqrt(float x)
root = intial guess
while root^2 is not close enough to x do
  root = (x / root + root) / 2;
return root;

Wikipedia has a nice article with more details and in particular how
to get a good initial guess.

http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Taylor_series

On Sep 25, 8:29 am, Vikram Singh <singhvikram...@gmail.com> wrote:
> ok.. thanks guys...
> one doubt now... if this ques is asked in an interview(its already been
> asked in ms interview)... then u cant just write the code... u hv to explain
> the whole approach like why u r choosing that way to narrowing dowm the
> range...
>
> so pls explain how this sol is derived...
>
> On Sun, Sep 25, 2011 at 10:15 AM, keyan karthi 
> <keyankarthi1...@gmail.com>wrote:
>
>
>
> > binary search!!! :)
>
> > On Sat, Sep 24, 2011 at 11:38 PM, sunny agrawal 
> > <sunny816.i...@gmail.com>wrote:
>
> >> let x be the number
> >> initialize ans to some value like x or x/2
>
> >> now repeatedly do the following
> >> ans  = (ans + x/ans)/2
> >> each time you perform this operation you will move closer to the sqrt
> >> value and depending upon the precision required stop
>
> >> On Sat, Sep 24, 2011 at 11:17 PM, siddharth srivastava <
> >> akssps...@gmail.com> wrote:
>
> >>> On 24 September 2011 13:45, shady <sinv...@gmail.com> wrote:
>
> >>>> one of the simplest way is to do binary search... :)
>
> >>> +1
>
> >>>> On Sat, Sep 24, 2011 at 8:42 PM, teja bala 
> >>>> <pawanjalsa.t...@gmail.com>wrote:
>
> >>>>>http://www.geeksforgeeks.org/archives/3187
>
> >>>>> check dis one.............
>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google
> >>>>> Groups "Algorithm Geeks" group.
> >>>>> To post to this group, send email to algogeeks@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to
> >>>>> algogeeks+unsubscr...@googlegroups.com.
> >>>>> For more options, visit this group at
> >>>>>http://groups.google.com/group/algogeeks?hl=en.
>
> >>>>  --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "Algorithm Geeks" group.
> >>>> To post to this group, send email to algogeeks@googlegroups.com.
> >>>> To unsubscribe from this group, send email to
> >>>> algogeeks+unsubscr...@googlegroups.com.
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/algogeeks?hl=en.
>
> >>> --
> >>> Regards
> >>> Siddharth Srivastava
>
> >>>  --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Algorithm Geeks" group.
> >>> To post to this group, send email to algogeeks@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> algogeeks+unsubscr...@googlegroups.com.
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/algogeeks?hl=en.
>
> >> --
> >> Sunny Aggrawal
> >> B.Tech. V year,CSI
> >> Indian Institute Of Technology,Roorkee
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to algogeeks@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> algogeeks+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/algogeeks?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algogeeks@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to