Skip to content

Instantly share code, notes, and snippets.

@mhoran
Last active January 1, 2016 11:49
Show Gist options
  • Save mhoran/8140818 to your computer and use it in GitHub Desktop.
Save mhoran/8140818 to your computer and use it in GitHub Desktop.
int yearsToRetirementHelper(float principal, float rateOfReturn, float annualPostTaxIncome, float annualExpenses, int yearsToRetirementAcc) {
float returnOnInvestment = principal * rateOfReturn;
if (returnOnInvestment > annualExpenses) {
return yearsToRetirementAcc;
} else {
float newStash = (principal + returnOnInvestment + annualPostTaxIncome) - annualExpenses;
return yearsToRetirementHelper(newStash, rateOfReturn, annualPostTaxIncome, annualExpenses, yearsToRetirementAcc + 1);
}
}
int yearsToRetirement(float principal, float rateOfReturn, float annualPostTaxIncome, float annualExpenses) {
return yearsToRetirementHelper(principal, rateOfReturn, annualPostTaxIncome, annualExpenses, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment