Tell us what’s happening:
cant pass 22 and 23
22. square_root_bisection(225, 1e-7, 10) should return None.
- square_root_bisection(225, 1e-7, 10) should print Failed to converge within 10 iterations.
can get help in what direction i should head in (aware that the print and return c at the end is redundant i just didn’t have another place to put them for now)
Your code so far
def square_root_bisection(no,tol=1e-5,max_iter=5):
if no < 0:
raise ValueError('Square root of negative number is not defined in real numbers')
elif no == 0 or no == 1:
print(f'The square root of {no} is {no}')
return no
elif no > 0:
cur_iter=0
if no <1:
a= no
b=1
else:
a =1
b = no
while cur_iter<=max_iter:
c=(a+b)/2
while abs(b-a) >= tol:
if c**2 < no:
a=c
else:
b=c
c=(a+b)/2
cur_iter +=1
print(f'The square root of {no} is approximately {c}')
return c
print(f'Failed to converge within {max_iter} iterations' )
return None
square_root_bisection(0)
square_root_bisection(0.001, 1e-7, 15)
square_root_bisection(225, 1e-7, 20)
square_root_bisection(225, 1e-7, 10)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method