# Recipe 5.17 Calculating Retirement Savings

#### 5.17.1 Problem

You want to calculate how much money you'll have at retirement, assuming you'll receive a series of equal periodic payments.

#### 5.17.2 Solution

Calculate the future value of a series of periodic payments using the custom Math.FV( ) function.

#### 5.17.3 Discussion

Suppose you are implementing a retirement calculator. People commonly deposit a certain amount of money each month into their retirement account. Typically, they want to know how much money they'll accumulate in the account at some time in the future. There are two components to the accumulation of funds: the interest earned each period, plus the periodic payment that is added. The total value also includes a third component: the principal in the account at the beginning of the calculation.

Here is an enhanced version of the futureValue( ) function presented in Recipe 5.16. It calculates the future value based on an initial payment plus periodic payments. We calculate the final amount by adding together the appreciation of the principal and the appreciation of the periodic payments. If the periodic payment is 0, the calculation reduces to the equation from Recipe 5.16 (in which there is principal and interest only). If there is no initial deposit, you can use 0 for the PV parameter. You can add this to your Math.as file to use it in other projects.

```Math.FV = function (interest, n, PMT, PV) {
// PV       = initial deposit (present value)
// interest = periodic interest rate
// n        = number of payment periods
// PMT      = periodic payment
// FV       = future value

// Financial equations for a series of payments compounded over n periods:
// multiplier      = Math.pow ((1 + interest ), n)
// FV of principle = PV * Math.pow ((1 + interest ), n)
//                 = PV * multiplier
// FV of payments  = PMT * ((Math.pow ((1 + interest ), n) - 1) / interest)
//                 = PMT * ((multiplier-1)/interest)

multiplier = Math.pow ((1 + interest), n);
// Add FV of principal and FV of payments together to get total value.
FV = PV * multiplier + PMT * ( (multiplier-1) / interest);

return FV;
};```

You can use the custom Math.FV( ) method as follows:

```// How much money will I have in 30 years if it appreciates at 6%, starting with
// \$50,000 and paying \$400/month?
trace (Math.FV(.06/12, 30*12, 400, 50000));```

Again, you can consult a financial textbook for the derivation of the preceding formulas. You can confirm their accuracy by testing them against the value derived from the brute-force method:

```Math.bruteFV = function (interest, n, PMT, PV) {
FV = PV;
for (var periods = interest; periods <= n; periods++) {
FV = FV * (1 + interest) + PMT;
}
return FV;
};```

 You can and should confirm your calculations against the values returned by a commercial spreadsheet or actuarial table. For example, our custom Math.FV( ) function returns the same value, as the one obtained in Microsoft Excel using the Insert Function Financial FV formula.