I've created an algorithm on python to find the approximate square root of a number but I'm interested to see if there are any shortcuts I can make. I have a while loop that needs to call back to a statement before the loop. For example, it starts at 2, goes to 6, then loops back to 1, up to 6, back to 1, etc. I tried using an i variable that works but not to sure if its the most efficient. So essentially can anyone help me in terms of making this algorithm more efficient, thanks.
def rooter(A , R):
i = 0
if A <= 0:
print("Invalid number")
return -1
(c) = (A / R)
while abs(R - c) > 0.01:
if i > 1:
(c) = (A / R)
s = (1/2) * (R + c)
R = s
i +=1
answer = (round(R, 3))
print(answer)
rooter(4, 100)
R represents the accuracy of the calculation.