Frontier Software

Robert Laing's programing notes

Dynamic Programing

By Robert Laing

Geometric series (eg interest rates)

$$ y = a + ar + ar^2 + \ldots + ar^n = \sum_{x = 0}^n ar^x $$

A JavaScript function to do the above could be written as:

function future_value1(a, r, n) {
  let i;
  for (i = 0; i < n; i++) {
    a = a + (a * r);
  }
  return a;
}

A more efficient way is to use the formula:

$$ y = a + ar + ar^2 + \ldots + ar^n \\ ry = ar + ar^2 + \ldots + ar^{n+1} \\ y(1 - r) = a(1 - r^{n+1}) \\ y = a \frac{(1 - r^{n+1})}{(1 - r)} $$
function future_value2(a, r, n) {
  return a * (1 - r**(n+1))/(1 - r);
}
Last updated on 29 Aug 2021
Published on 29 Aug 2021

Content Footer