Augustin Grudet

Compound interest: the formula, the code and the curve

Sample post used as a test bench: maths, code, chart, footnotes and sources.

Contents

This post is a reference to check that the whole content pipeline works: maths, code, charts and footnotes1.

The formula

Capital C0C_0 invested at annual rate rr, compounded nn times a year for tt years, is worth:

C(t)=C0(1+rn)ntC(t) = C_0 \left(1 + \frac{r}{n}\right)^{nt}

As n→∞n \to \infty, we get continuous compounding C(t)=C0ertC(t) = C_0 e^{rt}.

In code

def compound(c0: float, r: float, years: int, n: int = 12) -> float:
    return c0 * (1 + r / n) ** (n * years)

print(round(compound(10_000, 0.07, 30), 2))

The curve

Figure 1 compares three rates over forty years. The gap looks modest at first, then widens: that is the exponential at work.

  • 3%
  • 5%
  • 7%
020,00040,00060,00080,000100,000120,000140,000160,000Capital (€)0510152025303540Years050,000100,000150,000Capital (€)010203040Years
€10,000 invested at 3%, 5% and 7% a year, compounded annually, over 40 years.

Taking logarithms, ln⁡C(t)=ln⁡C0+tln⁡(1+r)\ln C(t) = \ln C_0 + t \ln(1 + r): on a log scale each curve becomes a straight line of slope ln⁡(1+r)\ln(1 + r), as figure 2 shows.

  • 3%
  • 5%
  • 7%
10,00020,00050,000100,000Capital (€)0510152025303540Years10,00020,00050,000100,000Capital (€)010203040Years
Same data on a log scale: straight lines of slope ln(1 + r).

Orders of magnitude

Final capital (€) of €10,000 invested by annual rate and duration: C = 10,000 × (1 + r)^t, rounded to the euro.
Duration1%3%5%7%10%
10 years11,04613,43916,28919,67225,937
20 years12,20218,06126,53338,69767,275
30 years13,47824,27343,21976,123174,494
40 years14,88932,62070,400149,745452,593

Notes

  1. Footnotes are supported natively by Markdown. ↩

Sources

  1. Compound interest, Wikipedia (Nov 25, 2026)