Question: (In Python, beginner level: using a while loop) Your program should include three Python functions. sqrt_compare will call mysqrt, to compute the approximate square root

(In Python, beginner level: using a while loop)

Your program should include three Python functions. sqrt_compare will call mysqrt, to compute the approximate square root for a given number, and then compare this result with the square root returned by the sqrt function from the math module in the Python Standard Library. Finally, you will write and call a main function to execute your program.

My square root must follow the babylonian method:

X(k+1) = (1/2) * (Xk + n/Xk), where X0 = 1

mysqrt will have two parameters, n, a positive integer to find the square root of, and k, the number of times the iterative square root approximation process should run. The function should use the equation given above to determine the approximate square root of n, and return (note: not print) this result. mysqrt should return correct values for the simple examples included in your docstring, and also for the test cases given here:

>>> mysqrt(25, 5) 5.000023178253949

>>> mysqrt(25, 10) 5.0

>>> mysqrt(100, 10) 10.0

>>> mysqrt(625, 10) 25.0

sqrt_compare will have two parameters, num, a positive integer to find the square root of, and iterations, the number of times the iterative square root approximation process should run. sqrt_compare will call mysqrt (with num and iterations as arguments) and then compare the value returned by mysqrt to the square root of n returned by math module sqrt. sqrt_compare should report on (print) the difference between the two values as a percentage error (the absolute value of math sqrt result - mysqrt result, divided by math sqrt result, then multiplied by 100) rounded to 2 decimal places. sqrt_compare should return None. sqrt_compare should yield the following, for example:

>>> sqrt_compare(10000, 8)

For 10000 using 8 iterations:

mysqrt value is: 101.20218365353946

math lib sqrt value is: 100.0

This is a 1.2 percent error.

Finally, write a main function. The main function will not have any parameters, and will return None. For the main function (ONLY), you do not need any documentation beyond a brief description. The main function should call function sqrt_compare for each of the seven examples given above. Then, call function main to execute your program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!