Implement the Bisection Method - Implement the Bisection Method

Tell us what’s happening:

Hi, I’m stuck on the final step as I have no idea what the logic is supposed to be here. (Like how are we expected to interpret this 1e-7 nonsense…)

I’ve made the code print out the result of every iteration and realise that if I change my tolerance to anything with more than 2 decimal places, the entire thing breaks and does not run and it completely stumps me. I must’ve overlooked something for sure. Can anyone help? :folded_hands::folded_hands:

Your code so far

def square_root_bisection(number,tolerance=0.01,iterations=1):

    if number<0:
        raise ValueError('Square root of negative number is not defined in real numbers')
        
    elif number==0 or number==1: 
        print(f"The square root of {number} is {number}")
        return number

    else:
        results=''
        lower_bound=0

        if number>1:
            upper_bound=number

        else: #number<1

            upper_bound=1    

        mid=0

        for i in range(iterations):


            if upper_bound-lower_bound>tolerance:
                mid = (upper_bound+lower_bound)/2
                square=mid**2
                results+=f'{i+1} : Root:{mid} High:{upper_bound} Low:{lower_bound} Difference: {upper_bound-lower_bound}\n\n'

                if square!=number:

                    if square<number:
                        lower_bound = mid

                    if square>number:
                        upper_bound = mid
                else:
                    print(f'The square root of {number} is approximately {mid}')
                    print(results)
                    return mid

            else:
                print(f'The square root of {number} is approximately {mid}')
                print(results)
                return mid

        




square_root_bisection(9,0.1, 10)

Your browser information:

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

Challenge Information:

Implement the Bisection Method - Implement the Bisection Method

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-bisection-method/686ccc2c8b967e17ab18d593.md at main · freeCodeCamp/freeCodeCamp · GitHub

1e-7 is scientific notation, the number is 0.0000001

which part of your code deals with the “Failed to converge” thing?

Hi @Llerrad,

Please read again this part of the instructions:

It 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.

You are only iterating once?

Happy coding!

that is not true, it’s only the default value, the number of iterations is established by the function call

for example here it’s 10 iterations

True. Thanks for clarifying. Getting more coffee… :yawning_face: