I have this assignment: \"First, design and implement a Python program that take
ID: 3757765 • Letter: I
Question
I have this assignment: "First, design and implement a Python program that takes a positive whole number n as input and outputs the square root of n using the Babylonian algorithm. The Babylonian algorithm computes the square root of a positive number, n, as follows: Make a guess at the answer (you can pick n/2 as your initial guess). Compute r = n / guess Set guess = (guess + r) / 2 Go back to step 2 for as many iterations as necessary. The more steps 2 and 3 are repeated, the closer guess will become to the square root of n. Compare your calculated square root with the math.sqrt() result."
but i do not understand it. It then asks: "What are the steps/equations used to find the square root? How are you going to address how to stop the algorithm? Are you going to ask for the number of iterations? Are you going to give it a threshold?"
I think if i were able to answer those questions i would be able to write the code on my own. but i am confused as to where to even begin.
thank you in advance!
Explanation / Answer
def BabylonianAlgorithm(number):
if(number == 0):
return 0;
guess = number/2.0;
g2 = guess + 1;
while(g != g2):
n = number/ g;
g2 = guess;
g = (guess + n)/2;
return guess;
print 'The Square root of 0.3 =', BabylonianAlgorithm(0.3);
for i in range(1, 11):
print 'The Square root of ', i ,' =', BabylonianAlgorithm(i);