function x = MySqrt(A)
% x = MySqrt(A)
% A is a non-negative real and x is its square root.

if A==0
x = 0;
else
% Write A = m*4^e where 1<=m<=4 for then sqrt(A) = sqrt(m)*2^e
TwoPower = 1;
m = A;
while m>=1
m = m/4;
TwoPower = 2*TwoPower;
end
while m < .25
m = m*4;
TwoPower = TwoPower/2;
end
% sqrt(A) = sqrt(m)*TwoPower
% L(x) = 1/3 + (2/3)x interpolates the sqrt function at x = 1 and x = 4.
% Initial guess = L(m).
x = (1+2*m)/3;
% The rectangle averaging process....
for k=1:4
x = (x + (m/x))/2;
end
x = x*TwoPower;
end