@Ankuj: Yeah, but he asked for it to be recursive. Yours is iterative.

Dave

On Aug 9, 9:56 am, Ankuj Gupta <[email protected]> wrote:
> we can do it in logn by using binary search approach found
> n is the number whose square root has to be
>
>         if(n==1)
>                 return 1;
>         if(n==0)
>                 return 0;
>         int low=0,high=n/2,mid,temp;
>         while(1)
>         {
>                 mid = (low+high)/2;
>                 temp = mid*mid;
>                 if(temp==n)
>                         return 1;
>                 else if(temp <n)
>                         low = mid+1;
>                 else
>                         high = mid-1;
>                 if(low == mid || high == mid)
>                         return 0;
>         }
> at the end high will have the required square root if not perfect
> square or if perfect square mid will have the required answer

-- 
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.

Reply via email to