Binary Search kind of mathod is useful here :
float SquareRoot(float n,float start,float end)
{
float s=(start+end)/2;
if(n - sqr(s) < 0.001) && (n - sqr(s) > -0.001))
return (end+start)/2;
else if(sqr(s) > n)
return SquareRoot(n,0.0,s);
else
return SquareRoot(n,s,end);
}
Sanju
:)
On Tue, Aug 30, 2011 at 3:25 AM, UTKARSH SRIVASTAV
<[email protected]>wrote:
> i don't whethe you have studied a subject cbnst from that use newton
> raphson method
>
>
> On Tue, Aug 30, 2011 at 2:39 AM, Ankuj Gupta <[email protected]> wrote:
>
>> U can use binary search method
>>
>> On Aug 30, 1:56 pm, Rajeev Kumar <[email protected]> wrote:
>> > use Babylonian method(Efficient) algrithm..............
>> > Refer :
>> http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylo...
>> >
>> > public *void* getSquareRoot(double s) {
>> > double Xn = 2.0;
>> > double lastXn = 0.0;
>> > while (Xn != lastXn) {
>> > lastXn = Xn;
>> > Xn = (Xn + s / Xn) / 2.0;
>> > }
>> > return Xn;
>> > }
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > On Tue, Aug 30, 2011 at 1:49 PM, Ankur Garg <[email protected]>
>> wrote:
>> > > @techcoder
>> >
>> > > Making an array of 32768 or INT_MAX will make ur compiler cry
>> >
>> > > Also ur case doesnt handle the scenario where square root is a decimal
>> > > number
>> >
>> > > On Tue, Aug 30, 2011 at 1:35 PM, tech coder <
>> [email protected]>wrote:
>> >
>> > >> the sqrt of 32 bit number can't be more than 16 bits.
>> >
>> > >> have an array of 2^16 elemnts wtih elemts 1 2 3 4 5 .... 32768 .
>> >
>> > >> now apply binary search
>> > >> i=a[mid] where mid=(lower+upper)/2
>> >
>> > >> if(i*i==num)
>> > >> i is the sqrt
>> >
>> > >> increment lower and upper accordingly as we do in binary search
>> >
>> > >> so order is Ologn where n=2^16
>> >
>> > >> On Tue, Aug 30, 2011 at 11:37 AM, Raghavan <[email protected]>
>> wrote:
>> >
>> > >>> how to design this logic effectively?
>> >
>> > >>> double squareRoot(int num){
>> >
>> > >>> }
>> >
>> > >>> --
>> > >>> Thanks and Regards,
>> > >>> Raghavan KL
>> >
>> > >>> --
>> > >>> You received this message because you are subscribed to the Google
>> Groups
>> > >>> "Algorithm Geeks" group.
>> > >>> To post to this group, send email to [email protected].
>> > >>> To unsubscribe from this group, send email to
>> > >>> [email protected].
>> > >>> 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 [email protected].
>> > >> To unsubscribe from this group, send email to
>> > >> [email protected].
>> > >> 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 [email protected].
>> > > To unsubscribe from this group, send email to
>> > > [email protected].
>> > > For more options, visit this group at
>> > >http://groups.google.com/group/algogeeks?hl=en.
>> >
>> > --
>> > Thank You
>> > Rajeev Kumar
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>
>
> --
> *UTKARSH SRIVASTAV
> CSE-3
> B-Tech 3rd Year
> @MNNIT ALLAHABAD*
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.