You can calculate this efficiently using a numerical method which is a twist on binary search.
1- Let z = 0
2- Let y = x+z/2
3- Calculate y^y If equal return y else if smaller than x let z = y else let x = y
4- Repeat
Anonimo
22 apr 2012
based on netwon raphson method;
x(n+1)=x(n)+a/x(n); is the recursion equation for sqrt(a);
double sqrt(int a, int err)
{
if(aerr)
{
prev=ans;
ans=ans+(a*1.0/ans);
diff=ans-prev;
diff=diff<0?-diff:diff;
diff/=prev;
}
return ans;
}
Anonimo
22 apr 2012
a couple of mistakes
ans=(ans+a/ans); replaced by ans=(ans+a/ans)*0.5;
and err is double not int
sorry
Anonimo
9 nov 2012
http://collabedit.com/tuvng
Anonimo
2 apr 2012
x to the power 1/2 is sqrt(x) - so implement sqrt of x