Skip to main content

The Ramanujan Principle

Showing and telling
Principle or heuristic
Intermediate
Tables are read as crude graphs.
Published

November 9, 2026

Modified

November 13, 2026

Entry #35 in Andrew Gelman’s Handy Statistical Lexicon. Original discussion: Tables as graphs: the Ramanujan principle.

Filed under Showing and telling · Principle or heuristic · Intermediate.

Source check: ✓ The explanation was checked against the source.

What it is

Gelman’s post: “Tables are commonly read as crude graphs: what you notice in a table of numbers is (a) the minus signs, and thus which values are positive and which are negative, and (b) the length of each number, that is, its order of magnitude.” The name comes from the most famous example: “the mathematician Srinivasa Ramanujan supposedly conjectured the asymptotic form of the partition function based on a look at a table of the first several partition numbers: he was essentially looking at a graph on the logarithmic scale.”

Ramanujan is not an exception to the rule but its best illustration. Everyone reads tables this way, and he read one so well that he saw the formula behind it. Gelman discusses modern statistical examples in an article for Significance magazine.

Why it matters

If readers take in a table through its signs and the lengths of its numbers, then those are the features a table should make easy to see. A regression table with six decimal places, unsorted rows and ragged alignment hides the signs and orders of magnitude among digits that carry no information. A table designed with this in mind (rounded, aligned, sorted, with signs easy to spot) works as the crude graph readers will make of it anyway. When a comparison matters more than that, draw the actual graph.

How it works

The number of digits in a positive integer \(x\) is \(\lfloor \log_{10} x \rfloor + 1\), so a right-aligned column of integers is a bar chart on the log scale. Ramanujan’s table was of the partition numbers \(p(n)\), the number of ways to write \(n\) as a sum of positive integers: \(p(4) = 5\), because \(4 = 3+1 = 2+2 = 2+1+1 = 1+1+1+1\). Their lengths grow like \(\sqrt{n}\), and the asymptotic formula that Hardy and Ramanujan proved is

\[ p(n) \sim \frac{1}{4n\sqrt{3}} \exp\!\left(\pi\sqrt{\frac{2n}{3}}\right). \]

Simulated example

There is nothing random here: the table below holds the exact partition numbers, computed with Euler’s recurrence. Read it as Ramanujan might have, by the lengths of the numbers, then compare it with the graph on the log scale and the asymptotic formula.

Show simulation code

The R version uses base R, and the Python version uses NumPy and Matplotlib. Where the simulation draws random numbers, each language draws its own, so exact numbers differ slightly but show the same pattern. The numbered markers in the R code are explained under it.

import {range, fmt} from "../js/stats.js"

viewof nMax = Inputs.range([20, 200], {value: 100, step: 10, label: "Largest n"})
viewof showFormula = Inputs.toggle({label: "Show the Hardy–Ramanujan formula", value: true})

partitions = {
  // Euler's pentagonal-number recurrence, in exact integer arithmetic.
  const p = [1n];
  for (let n = 1; n <= 200; n++) {
    let s = 0n;
    for (let k = 1; k * (3 * k - 1) / 2 <= n; k++) {
      const sign = k % 2 ? 1n : -1n, g1 = k * (3 * k - 1) / 2, g2 = k * (3 * k + 1) / 2;
      s += sign * p[n - g1];
      if (g2 <= n) s += sign * p[n - g2];
    }
    p.push(s);
  }
  return p;
}
asymptotic = n => Math.exp(Math.PI * Math.sqrt(2 * n / 3)) / (4 * n * Math.sqrt(3))
curve = range(nMax).map(i => i + 1).map(n => ({n, exact: Math.log10(Number(partitions[n])), formula: Math.log10(asymptotic(n))}))

html`<table class="table table-sm" style="max-width: 420px; font-family: var(--bs-font-monospace);">
<thead><tr><th style="text-align:right">n</th><th style="text-align:right">p(n)</th></tr></thead>
<tbody>${range(nMax / 10).map(i => (i + 1) * 10).map(n => html`<tr><td style="text-align:right">${n}</td><td style="text-align:right">${partitions[n].toString()}</td></tr>`)}</tbody></table>`

Plot.plot({
  ariaLabel: "The Ramanujan Principle: partition numbers on the log scale",
  ariaDescription: "Horizontal axis: n. Vertical axis: base-10 logarithm of the partition number p(n), roughly its number of digits. Dots show the exact values and a line shows the Hardy–Ramanujan formula. The chart updates with the controls above it, and the summary below the charts states the result in words.",
  height: 300,
  x: {label: "n", grid: true},
  y: {label: "log₁₀ p(n), about the number of digits", grid: true},
  marks: [
    Plot.dot(curve, {x: "n", y: "exact", r: 2.5, fill: "#2166ac"}),
    showFormula ? Plot.line(curve, {x: "n", y: "formula", stroke: "#d6604d", strokeWidth: 2}) : null
  ]
})
# The Ramanujan Principle: partition numbers and the Hardy-Ramanujan formula.
1n_max <- 100 # largest n
# Deterministic: no random numbers. Values up to n = 200 (about 4e12) fit
# exactly in R's double-precision numbers.

# Euler's pentagonal-number recurrence for p(n), the number of partitions
2p <- numeric(201)
p[1] <- 1 # p[n + 1] holds p(n)
for (n in 1:200) {
  k <- 1
  s <- 0
3  while (k * (3 * k - 1) / 2 <= n) {
    sgn <- if (k %% 2) 1 else -1
    g1 <- k * (3 * k - 1) / 2
    g2 <- k * (3 * k + 1) / 2
    s <- s + sgn * p[n - g1 + 1]
    if (g2 <= n) s <- s + sgn * p[n - g2 + 1]
    k <- k + 1
  }
  p[n + 1] <- s
}
4asymptotic <- function(n) exp(pi * sqrt(2 * n / 3)) / (4 * n * sqrt(3))

print(
  data.frame(
    n = seq(10, n_max, 10),
    p = format(p[seq(10, n_max, 10) + 1], big.mark = ",")
  ),
  row.names = FALSE
)
cat("Formula / exact at n = ", n_max, ": ",
  round(asymptotic(n_max) / p[n_max + 1], 3), "\n",
  sep = ""
)

5plot(1:n_max, log10(p[2:(n_max + 1)]),
  type = "l", lwd = 2,
  col = "#2166ac", xlab = "n", ylab = "log10 p(n)"
)
lines(1:n_max, log10(asymptotic(1:n_max)),
  col = "#d6604d", lwd = 2,
  lty = 2
)
1
The page’s control: the largest n to show. There is no seed: the computation is exact.
2
The exact partition numbers p(n), computed by recurrence.
3
Euler’s pentagonal-number recurrence, which gives each p(n) from earlier ones.
4
The Hardy-Ramanujan formula: a closed form that is only asymptotically right, and already close.
5
Exact values and the formula on a log scale. Ramanujan’s principle: a formula can be approximately right and still tell you the truth about the problem.
# The Ramanujan Principle: partition numbers and the Hardy-Ramanujan formula.
import math
import matplotlib.pyplot as plt

n_max = 100  # largest n
# Deterministic: no random numbers. Python integers are exact.

# Euler's pentagonal-number recurrence for p(n), the number of partitions
p = [1]
for n in range(1, 201):
    s, k = 0, 1
    while k * (3 * k - 1) // 2 <= n:
        sign = 1 if k % 2 else -1
        g1, g2 = k * (3 * k - 1) // 2, k * (3 * k + 1) // 2
        s += sign * p[n - g1]
        if g2 <= n:
            s += sign * p[n - g2]
        k += 1
    p.append(s)


def asymptotic(n):
    return math.exp(math.pi * math.sqrt(2 * n / 3)) / (4 * n * math.sqrt(3))


for n in range(10, n_max + 1, 10):
    print(f"p({n}) = {p[n]:,}")
print(f"Formula / exact at n = {n_max}: "
      f"{asymptotic(n_max) / p[n_max]:.3f}")

ns = range(1, n_max + 1)
plt.plot(ns, [math.log10(p[n]) for n in ns], color="#2166ac", linewidth=2,
         label="Exact")
plt.plot(ns, [math.log10(asymptotic(n)) for n in ns], "--",
         color="#d6604d", linewidth=2, label="Hardy-Ramanujan formula")
plt.xlabel("n")
plt.ylabel("log10 p(n)")
plt.legend()
plt.show()

Key takeaways

  • People read a table of numbers as a crude graph: first the minus signs, then the lengths of the numbers.
  • A right-aligned column of numbers is, in effect, a bar chart on the log scale. Ramanujan read one well enough to guess an asymptotic formula.
  • Design tables for that reading: round, align and sort, and make the signs easy to see. When the comparison really matters, draw the graph.

Source

Back to top