Exercise 7.1: Newton Square Root Loop
Given: a real value Y, and the formula for Newton's square root algorithm,
x(i) = (Y/x(i-1) + x(i-1))/2, where x(0)=Y/2;
Given: termination critierion: |Y - x(i)**2| < epsilon, a real value;
Compose: a for-initial loop that calculates successive approximations x(i) until the termination criterion is met, and returns the final value of the approximation as well as the number of approximations, x(i), i >0, that have been calculated.
for initial
x := Y / 2.0;
count := 0
while abs(Y - x*x) > epsilon repeat
x := (Y/old x + old x) / 2.0;
count := old count + 1
returns value of x
value of count
end for
Previous Section