Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Created February 3, 2018 20:09
Show Gist options
  • Save sreeprasad/b785881eb558470e57130fef78768d0c to your computer and use it in GitHub Desktop.
Save sreeprasad/b785881eb558470e57130fef78768d0c to your computer and use it in GitHub Desktop.
MIT: Introduction to Computer Science and Programming in Python : problem set 2 problem c
original_salary = float(input("Enter annual salary "))
house_cost_in_millions = int(input("Enter House cost in millions "))
house_cost = house_cost_in_millions * 1000000
down_payment = 0.25*house_cost
semi_annual_raise = 0.07
annual_investment_return = 0.04
delta = 101
low = 0
high = 1000
total_years = int(input("Enter total number of years "))
total_max_months = total_years * 12
savings = 0
steps =0
best_savings_rate = (low+high)/2.0
while(delta>100):
annual_salary = original_salary
monthly_salary = annual_salary/12
months=0
savings=0
while(months<total_max_months):
if (months!=0) and (months%6==0):
annual_salary = annual_salary + (annual_salary*semi_annual_raise)
monthly_salary = annual_salary/12
savings = savings + (savings*(annual_investment_return/12))
savings = savings + monthly_salary * (best_savings_rate/100)
months+=1
delta = abs(savings-down_payment)
if (savings>down_payment):
high = best_savings_rate
else:
low = best_savings_rate
best_savings_rate = (low+high)/2
steps+=1
if (best_savings_rate<=100):
print("best savings rate {:.4f}".format(best_savings_rate/100))
print("steps {}".format(steps))
save_annually = original_salary*(best_savings_rate/100)
print("You need to save ${:.4f} per year".format(save_annually))
else:
print("It is not possible to pay the down payment in three years")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment