Search

Search papers and lessons

Gamma Function

Level 2 · 9 min read

The gamma function extends the factorial from whole numbers to all real numbers (except zero and the negative integers), defined as an integral.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Sign in to see your progress

TL;DR

The factorial n!n! is defined only for whole numbers, and it is the product 1⋅2⋯n1 \cdot 2 \cdots n. The gamma function Γ(z)\Gamma(z) fills in the gaps: it is defined for every real and complex zz except zero and the negative whole numbers, and it equals (n−1)!(n-1)! at every positive whole number nn. It is defined as an integral, so it can be computed, differentiated and used in probability formulas.

Where you'll see it: in the normalising constants of the gamma, beta, Student-t and chi-squared distributions, in volumes of high-dimensional balls, and in the log-gamma function that software uses to avoid overflow.

Intuition

Counting products, then smoothing them

The factorial counts orderings. There are 3!=63! = 6 ways to line up three distinct objects, and 5!=1205! = 120 ways to line up five. The factorial grows by a fixed multiplicative step: each new integer multiplies the running product by one more factor. The question is what "multiply by nn" should mean when nn is 12\tfrac{1}{2} or π\pi.

The gamma function answers with a smooth curve through the factorial values. It is built so that the factorial's own rule still holds: going up by one multiplies the value by the new input. That single rule, Γ(z+1)=z Γ(z)\Gamma(z+1) = z\,\Gamma(z), together with Γ(1)=1\Gamma(1)=1 and a requirement that the curve be well behaved (the Bohr-Mollerup theorem makes this precise using log-convexity), pins the function down. Its values grow as fast as the factorial, and between the integers it bends smoothly.

123450510152025zΓ(z)
  • Γ(x)
The points mark Γ(n) = (n−1)! at whole numbers. Between them the curve is smooth. Near z = 0 it blows up, and it is undefined at zero and at the negative whole numbers.

The math

Definition: Gamma function

For real z>0z \gt 0, the gamma function is the integral

Γ(z)=∫0∞tz−1e−t dt.\Gamma(z) = \int_0^\infty t^{z-1} e^{-t}\,dt.

Here tt is a dummy variable, e−te^{-t} is the exponential function, and tz−1t^{z-1} is an ordinary power. The integral converges for every z>0z \gt 0. It extends to other values by the recurrence in the next subsection.

The recurrence

Take the integral for Γ(z+1)\Gamma(z+1) and integrate by parts (see Integration by Parts) with u=tzu = t^{z} and dv=e−t dtdv = e^{-t}\,dt, so du=z tz−1 dtdu = z\,t^{z-1}\,dt and v=−e−tv = -e^{-t}:

Γ(z+1)=[−tze−t]0∞+z∫0∞tz−1e−t dt=0+z Γ(z).\Gamma(z+1) = \left[-t^{z} e^{-t}\right]_0^\infty + z\int_0^\infty t^{z-1} e^{-t}\,dt = 0 + z\,\Gamma(z).

The boundary term vanishes at both ends for z>0z \gt 0: at t=0t = 0 because tz=0t^z = 0, and at infinity because the exponential wins. So

Γ(z+1)=z Γ(z).\Gamma(z+1) = z\,\Gamma(z).

Start with Γ(1)=∫0∞e−t dt=1\Gamma(1) = \int_0^\infty e^{-t}\,dt = 1. Applying the recurrence repeatedly gives Γ(2)=1⋅Γ(1)=1\Gamma(2) = 1 \cdot \Gamma(1) = 1, Γ(3)=2⋅1=2\Gamma(3) = 2 \cdot 1 = 2, and in general

Γ(n)=(n−1)!for whole numbers n≥1.\Gamma(n) = (n-1)! \qquad \text{for whole numbers } n \geq 1.

The shift by one is a historical convention: the integral's exponent is z−1z-1, so Γ(n)\Gamma(n) matches (n−1)!(n-1)! rather than n!n!.

Half-integer values

Set z=12z = \tfrac{1}{2} in the integral and substitute t=u2t = u^2, so dt=2u dudt = 2u\,du:

Γ(12)=∫0∞t−1/2e−t dt=2∫0∞e−u2 du=π.\Gamma\left(\tfrac{1}{2}\right) = \int_0^\infty t^{-1/2} e^{-t}\,dt = 2\int_0^\infty e^{-u^2}\,du = \sqrt{\pi}.

The last step uses the Gaussian integral ∫0∞e−u2 du=π/2\int_0^\infty e^{-u^2}\,du = \sqrt{\pi}/2 (half of ∫−∞∞e−u2du=π\int_{-\infty}^{\infty} e^{-u^2}du=\sqrt\pi, the integral behind the Gaussian (Normal) Distribution). Combining this with the recurrence gives every half-integer, for example Γ(32)=12π\Gamma(\tfrac{3}{2}) = \tfrac{1}{2}\sqrt{\pi}.

Negative values

Rearrange the recurrence to Γ(z)=Γ(z+1)/z\Gamma(z) = \Gamma(z+1)/z. For zz between −1-1 and 00 this extends the function, and the pole at each non-positive whole number comes from the division by zero. Those are the points where Γ\Gamma is undefined.

Worked examples

Example
Whole numbers by hand
  1. Start from the definition: Γ(1)=∫0∞e−t dt=1\Gamma(1) = \int_0^\infty e^{-t}\,dt = 1.
  2. Apply the recurrence with z=1z = 1: Γ(2)=1⋅Γ(1)=1\Gamma(2) = 1 \cdot \Gamma(1) = 1.
  3. With z=2z = 2: Γ(3)=2⋅Γ(2)=2\Gamma(3) = 2 \cdot \Gamma(2) = 2.
  4. With z=3z = 3: Γ(4)=3⋅Γ(3)=6\Gamma(4) = 3 \cdot \Gamma(3) = 6.
  5. Check: (4−1)!=3!=6(4-1)! = 3! = 6. Matches.

Interpretation: the gamma function at nn is the factorial at n−1n - 1, and the recurrence reproduces the factorial exactly at whole numbers.

Example
A half-integer and a volume

The volume of a unit ball in nn dimensions is Vn=πn/2/Γ ⁣(n2+1)V_n = \pi^{n/2} / \Gamma\!\left(\tfrac{n}{2} + 1\right). Take n=3n = 3, the ordinary ball.

  1. Compute the gamma value: Γ(52)=32 Γ(32)=32⋅12π=34π≈1.32934\Gamma(\tfrac{5}{2}) = \tfrac{3}{2}\,\Gamma(\tfrac{3}{2}) = \tfrac{3}{2} \cdot \tfrac{1}{2}\sqrt{\pi} = \tfrac{3}{4}\sqrt{\pi} \approx 1.32934.
  2. Compute the power: π3/2=ππ≈5.56833\pi^{3/2} = \pi \sqrt{\pi} \approx 5.56833.
  3. Divide: V3=5.56833/1.32934≈4.18879V_3 = 5.56833 / 1.32934 \approx 4.18879.
  4. Compare with the known formula 43π≈4.18879\tfrac{4}{3}\pi \approx 4.18879. Matches.

Interpretation: the gamma function gives the ball volume formula for every dimension, including the half-integer cases that the factorial cannot reach.

Tradeoffs and failure modes

SituationProblemBetter choice
Integer argument, small nnFactorial is exact and simpleUse n!n! directly
Large argument, such as z=200z = 200Γ(z)\Gamma(z) overflows the floating-point rangeUse the log-gamma function, lgamma in most libraries
Argument near zero or a negative whole numberValue is huge or undefinedCheck the domain before calling; use the reflection formula for negative values
Very large zz with asymptotics neededExact evaluation is wastefulStirling's approximation, Γ(z)≈2π/z (z/e)z\Gamma(z) \approx \sqrt{2\pi/z}\,(z/e)^z

Most numerical bugs with the gamma function are overflow bugs. A product of two gamma values can be fine on paper and produce infinity in code. Work in logs whenever the arguments are large.

History

Leonhard Euler introduced the function in the 1720s and 1730s while looking for a way to interpolate the factorial, and he worked it out in a letter to Christian Goldbach. Euler's integral form is the one used above. Adrien-Marie Legendre gave the capital Γ notation in the early 1800s, and the name "gamma function" stuck. The function caught on because it solved a real problem: it made the factorial usable in analysis, where non-integer and complex arguments are routine. Today it appears in almost every branch of applied mathematics, especially probability, where it sets the normalising constants of many distributions.

Check yourself

QuestionRecall: what is Γ(5)\Gamma(5), and what does it equal in factorial terms?Show answer

Γ(5)=4!=24\Gamma(5) = 4! = 24. The gamma function at nn equals the factorial at n−1n - 1.

QuestionComputation: use the recurrence and Γ(12)=π\Gamma(\tfrac{1}{2}) = \sqrt{\pi} to find Γ(52)\Gamma(\tfrac{5}{2}).Show answer

Γ(52)=32 Γ(32)\Gamma(\tfrac{5}{2}) = \tfrac{3}{2}\,\Gamma(\tfrac{3}{2}) and Γ(32)=12 Γ(12)=12π\Gamma(\tfrac{3}{2}) = \tfrac{1}{2}\,\Gamma(\tfrac{1}{2}) = \tfrac{1}{2}\sqrt{\pi}. So Γ(52)=34π≈1.32934\Gamma(\tfrac{5}{2}) = \tfrac{3}{4}\sqrt{\pi} \approx 1.32934.

QuestionWhy: why does the gamma function have poles at zero and the negative integers?Show answer

The recurrence Γ(z)=Γ(z+1)/z\Gamma(z) = \Gamma(z+1)/z divides by zz. At z=0z = 0 the division is by zero, and at each negative whole number the same thing happens once the chain of divisions reaches zero. The integral definition only works for z>0z \gt 0, so the poles are where the extension has to blow up.

QuestionApplied: a program computes Γ(300)\Gamma(300) directly and returns infinity. What is the fix?Show answer

The true value of Γ(300)=299!\Gamma(300) = 299! is far beyond the largest floating-point number, so the intermediate result overflows. Compute ln⁡Γ(300)\ln \Gamma(300) with a log-gamma routine instead, and combine it with other logs (for example, add and subtract logs in a likelihood) so the final answer is exponentiated only if it is small enough to represent.

Sources