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.