Do it more than once

Sometimes it is desirable to perform a computation several times. Consider a situation when you want to determine the future value of your savings, after a determined number of years into the future. Using some symbols and equations we can express this as an iterative computation as follows.

Assume that you invest an amount of a units of money, e.g. dollars, today, and that it is desired to find out how much this investment is worth after a number of years. Let the number of years be expressed using a variable n. If the interest rate is p percent, then the amount available after one year is $$ a_1 = a + a \cdot \frac{p}{100} \quad \quad \quad (5) $$ This equation can be rewritten, as $$ a_1 = a \left (1 + \frac{p}{100} \right ) \quad \quad \quad (6) $$ We see that, after two years, the amount becomes $$ a_2 = a \left (1 + \frac{p}{100} \right )^2 \quad \quad \quad (7) $$ and if we dare to generalize, we can figure out the amount after n years, as $$ a_n = a \left (1 + \frac{p}{100} \right )^n \quad \quad \quad (8) $$

Suppose now that the interest rate varies, as in the real life, from year to year. We can use the notation \(p_i\) for the interest rate during year number \(i\). Using a more advanced notation, we can now express the amount after n years as $$ a_n = a \prod_{i = 1}^n \left (1 + \frac{p_i}{100} \right ) \quad \quad \quad (9) $$

Continue reading in Section A fixed number of times

This the C view - other views are Java - Python