Skip to content

Compound Interest — A Software Engineer's Mathematical Guide (Interactive)

Posted on:February 24, 2025 at 10:00 AM

Every financial instrument in existence is built on one formula. Central banks use it to set policy. Banks use it to price loans. Pension funds use it to project returns. Most people who interact with it daily couldn’t derive it. Let’s fix that.

The formula

The discrete compounding formula — for interest applied n times per year:

A = P * (1 + r/n)^(n*t)

Where:

As n → ∞ (compounding infinitely often — continuously):

A = P * e^(r*t)

This follows from the definition of e: lim(n→∞) (1 + r/n)^n = e^r. The same e that appears everywhere in calculus, probability, and information theory. Compound interest is why e exists.


Compound Interest Explorer
The formula that Einstein (allegedly) called the eighth wonder of the world
Final value (nominal)
$81.66k
8.2x your money
Final value (real / inflation-adj)
$32.06k
Real rate: 3.88% (Fisher eq.)
Purchasing power eaten by inflation
$49.60k
60.7% of nominal gains
$10.0k$27.9k$45.8k$63.7k$81.7k0y3y6y9y12y15y18y21y24y27y30yAnnualMonthlyContinuousReal (continuous)Years

The gap between annual and continuous compounding shrinks as the rate increases. The dashed gold line is real purchasing power after 3% annual inflation — what your money actually buys.


What the tabs show you

Growth curves: Four lines — annual, monthly, continuous, and real (inflation-adjusted continuous). The gap between annual and continuous is the effective annual rate: e^r - 1. At 7% nominal, effective = 7.251%. At 15%, effective = 16.183%. The difference compounds itself.

Compounding frequency: Same rate, same time — different n. The surprise: the jump from annual to monthly matters far more than the jump from monthly to continuous. Going from n=1 to n=12 captures most of the benefit. Going from n=12 to n=∞ adds barely anything at typical rates.

Rule of 72 & math: The Rule of 72 estimates doubling time as 72 / r%. It’s derived from t = ln(2) / ln(1+r) ≈ 0.693/r for small r, with 72 chosen (instead of the mathematically exact 69.3) because it has more integer factors (convenient for mental math).

The Fisher equation: what your money actually buys

Nominal returns are lies. The real return — what your purchasing power actually grows by — follows the Fisher equation:

1 + r_real = (1 + r_nominal) / (1 + π)

Where π is inflation. The approximation r_real ≈ r_nom - π (which you see in introductory finance) has an error of about r_nom × π — negligible at 1-2% inflation, meaningful at 5-10%.

Try setting inflation to 8% (2022-level in Europe) and nominal return to 2% (typical savings account). The real return is negative: your money in the bank was purchasing-power-negative. The tool shows you exactly how much wealth was destroyed.

The software engineering angle

Compound interest is the simplest recursive function:

def compound(P: float, r: float, n: int, t: int) -> float:
"""Discrete: A = P * (1 + r/n)^(n*t)"""
return P * ((1 + r/n) ** (n * t))
# Continuous: special case as n → ∞
import math
def compound_continuous(P: float, r: float, t: int) -> float:
return P * math.exp(r * t)

The continuous case collapses to a single exp() call. In financial systems, this is used for:

Understanding the derivation — especially the limit lim(n→∞)(1+r/n)^n = e^r — makes these formulas feel obvious rather than magical. They’re all the same idea, parameterized differently.

The brutal truth about inflation

The chart makes this visceral. At 7% nominal return and 3% inflation over 30 years:

Inflation silently erodes ~47% of your nominal gains. The goal isn’t to maximize A — it’s to maximize A / P_inflation(t), the real value. This is why central banks targeting 2% inflation (not 0%) is a deliberate policy choice, not an oversight.

For the DeFi connection — how AMMs and yield farming interact with real interest rates — see the series starting with UniswapV2 math.