Implement the Bisection Method - Implement the Bisection Method

Tell us what’s happening:

I’ve been failing for 30 mins
Could someone tell me what the hell is wrong with 8 and 9 It say the iterations is the maximum difference from actual

Your code so far

def square_root_bisection(search_target, tolerance=0.000001, maximum_iterations=100):
    if search_target < 0:
        raise ValueError('Square root of negative number is not defined in real numbers')
    
    if search_target == 0:
        print('The square root of 0 is 0')
        return 0

    if search_target == 1:
        print('The square root of 1 is 1')
        return 1

    low = 0
    high = max(1, search_target)
    guess = (low + high) / 2
    iterations = 0
    
    
    
    while abs(guess**2 - search_target) > tolerance and iterations < maximum_iterations:
        if guess**2 < search_target:
            low = guess
        else:
            high = guess
        
        guess = (low + high) / 2
        iterations += 1
    
    # Check if converged within maximum iterations
    if abs(guess**2 - search_target) > tolerance:
        print(f"Failed to converge within {maximum_iterations} iterations")
        return None
    
    print(f"The square root of {search_target} is approximately {guess}")
    return guess
    

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0

Challenge Information:

Implement the Bisection Method - Implement the Bisection Method

Hi @orion_pax ,

The key bit of information for this challenge is from this portion of the instructions:

[The binary search method] works by narrowing down an interval where the square root lies until it converges to a value within a specified tolerance.

For example, if the tolerance is 0.01, the bisection method will keep halving the interval until the difference between the upper and lower bounds is less than or equal to 0.01.

Check what you are comparing to the tolerance against this instruction.

Happy coding!