function x = sqrt1(A)
% a function that finds the square root by scaling
% back to the interval [1/4,1]; using an initial
% guess which differs from the actual square root
% by less than 1/3, Newton's method gets the root
% to machine precision within at most 4 iterations
% (number of accurate digits doubles with each step)
% A>= 0, x>= 0
if A < 0 break;end
if A == 0
x = 0;
else
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
x = (1+2*m)/3; %optimal linear approx. in [.25,1]
for k = 1:4
x = (x+ (m/x))/2;
end
x = x*TwoPower; % scale back to original
end