Can someone help me to write using factorial to modify this algorithms?? Exercis
ID: 3745096 • Letter: C
Question
Can someone help me to write using factorial to modify this algorithms?? 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 midExplanation / Answer
Algo :
low=1
high=n
sqrtValue=n
function MySqrt(n){
if (n<=1){
Print n // For Intial Check 0 or 1
Return ;
}
mid=[(low+high)/2];
if(mid*mid<=sqrtValue and ((mid+1)*(mid+1)>sqrtValue))
Print mid ;
return ;
}
if(mid*mid<=sqrtValue){
low=mid
}
else{
high=mid
}
MySqrt(high); //Calling function again with updated value.
}
Test Case :
1 . if n=2 : returns around 1 value ;
2. if n=4 :
Mysqrt(4)->low=1,high=4,n=4
3.if n=9 :
Mysqrt(9)->low=1,high=9,n=9,mid=5 (mid*mid>n then high will be updated and n will be 5 ).
Mysqrt(5)->low=1,high=5,n=5,mid=3(sqrt is 3 ).
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.