Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2.2. The algorithm Algorithm MySart (Algorithm 4) is iterative. Modify

ID: 3743956 • Letter: E

Question

Exercise 2.2. The algorithm Algorithm MySart (Algorithm 4) is iterative. Modify it to make it as a recursive algorithm. Algorithm 4 Algorithm MySqrt(n). Input: An natural number n. Output: Lv/m 1: low = 1 // Low value of guess 2: high-n l/ High value of guess 3: while true do 4: m(low + high)/2] //current estimate for value of square root 5: if (mid * miK n) and ((mid + 1) * (mid + 1) > n) then 6: Output mid and STOP // this is the answer 7: else if (mid mid n) then 9: low mid 10: else 11: 12: //mid > lvii high mid

Explanation / Answer

// n is the number whose square root is to be calculated

// initial value of low is 1

// initial value of high is n

Algorithm MySqrt(n,low,high)

mid = floor( (low + high)/2 ) // current estimate for value of square root

if( (mid*mid <=n) and (mid+1 * mid+1 ) > n ) then

return mid //returns the answer

else if ( mid *mid <=n )

return MySqrt(n,mid,high) // change the value of low to mid and call the function

else

return MySqrt(n,low,mid) // change the value of high to mid and call the function