How to do a bisection search in python (MIT 6.0001)

164 Views Asked by At

I have tried to function for part c of the MIT 6.0001 PSET 1, but this function always prints zero for the saving rate. What is wrong with the function I have written such that it always prints zero? I have attached a link of the PSET 1.

https://ocw.mit.edu/courses/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/8cf75481d7047180c386de3e485bd050_MIT6_0001F16_ps1.pdf

def house3(annual_salary):
    
    low=0
    high=10000
    rate=(low+high)/2
    total_cost=10**6
    down_payment=0.25*total_cost
    savings=0
    months=0
    while abs(savings-down_payment)>100:
        rate=(low+high)/2

        for x in range(37):
            savings+=(savings*(0.04/12))+(annual_salary/12)*(rate/10000)
            if months%6==0:
                annual_salary+=annual_salary*0.07
            months+=1
        if savings<down_payment:
            low=rate
        elif savings>down_payment:
            high=rate
    print(rate)
0

There are 0 best solutions below