'Trying to solve MIT OCW Pset 1.C. I don't get the desired output
I was trying to solve the MIT OpenCourseWare Problem Set 1 C and I can't figure out why I'm not getting the output I require. The code is based on bisection search and it needs me to find the percentage of salary to save every month. Can someone help me understand where I'm going wrong?
r = 0.04
current_savings = 0.0
annual_salary = float(input('Enter your Annual Salary: '))
total_cost = 1000000
semi_annual_raise = 0.07
portion_down_payment = 0.25 * total_cost
months = 0
counter = 0
epsilon = 100
low = 0
high = 1
guess = (high + low) / 2.0
def calc(test):
months = 0
current_savings = 0
global portion_down_payment
global annual_salary
global semi_annual_raise
global r
while current_savings < portion_down_payment:
if months % 6 == 0 and months != 0:
annual_salary += annual_salary * semi_annual_raise
current_savings += (current_savings * r / 12)
current_savings += ((annual_salary / 12) * test / 1)
months += 1
if months > 36:
break
return current_savings
while abs(current_savings - portion_down_payment) >= epsilon:
current_savings = calc(guess)
counter += 1
if calc(guess) < portion_down_payment:
low = guess
else:
high = guess
guess = guess = (high + low) / 2.0
print(guess, counter)
Input
150000
Expected Output
0.441, 12
Output received
1.4349296274686127e-42 138
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
