Tell us what’s happening:
i tried newton algorithm for bisection and don’t understand why my code not pass in tests 22 and 23.
Your code so far
def square_root_bisection(number_sqrt, tolerance=0.01, iterations=10):
if number_sqrt < 0:
raise ValueError("Square root of negative number is not defined in real numbers")
if number_sqrt == 0 or number_sqrt == 1:
print(f"The square root of {number_sqrt} is {number_sqrt}")
return number_sqrt
if number_sqrt > 0:
guess = number_sqrt/2
last_result = (guess+number_sqrt/guess)*0.5
for iteration in range(iterations):
result = (last_result+number_sqrt/last_result)*0.5
print(result)
if abs(last_result - result) < tolerance:
print(f"The square root of {number_sqrt} is approximately {result}")
return result
last_result = result
print(f"Failed to converge within {iterations} iterations")
return None
print(square_root_bisection(0.001, 1e-7, 50))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method