Skip to content

Instantly share code, notes, and snippets.

@jpiccari
Last active August 29, 2015 14:02
Show Gist options
  • Save jpiccari/027e83ca507a06515e41 to your computer and use it in GitHub Desktop.
Save jpiccari/027e83ca507a06515e41 to your computer and use it in GitHub Desktop.
Created this function to help determine how much to contribute (annually) to my 401(k) to retire a decamillionaire.
/**
* Calculate the annual contributions required to reach a desired future value.
* @param {number} P - Current principle value.
* @param {number} FV - Desired future value.
* @param {number} r - Rate of return.
* @param {number} Y - Years of growth.
* @returns {number} Annual contribution.
*/
function annualContributions(P, FV, r, Y) {
var z = 1 + r;
return -(P * Math.pow(z, Y) - FV) / ((Math.pow(z, Y + 1) - z) / r);
}
// To become a millionaire by retirement with 40 years to contribute
annualContributions(0, 1e6, 0.12, 40); // 1163.9514133216733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment