What Pi Is and Why You Might Calculate It
Pi (π) is the ratio of a circle's circumference to its diameter — roughly 3.14159. You do not need to calculate it for everyday use; your calculator or programming language already has it built in. But mathematicians, programmers, and students calculate pi to understand how algorithms work, to test computer performance, or straightforward because the problem is interesting.
The methods range from geometry so straightforward you can do it with a ruler to formulas so complex they require thousands of lines of code. Which one you choose depends on how many decimal places you need, how much time you have, and what tools you have available.
Key Takeaways
- The simplest methods use geometry — measuring circles or using polygons — and give you pi to a few decimal places in minutes.
- The Leibniz formula and Machin's formula are straightforward to code but slow; they work well for learning how algorithms converge.
- The Bailey–Borwein–Plouffe formula and Chudnovsky algorithm are what modern computers use when they calculate pi to millions of digits.
- Your programming language (Python, Java, C) already has pi stored to machine precision, so you only calculate it if you need more digits or want to understand the method.
- Testing your calculation against a known value of pi tells you whether your code is correct.
The Geometry Method: Measuring a Circle
The oldest way to find pi is to draw a circle, measure its circumference and diameter, and divide one by the other. You need a piece of string, a ruler, and a circular object — a plate, a can, a coin.
Wrap the string around the edge of the circle as tightly as you can. Mark where the string meets itself. Lay the string flat and measure its length — that is the circumference. Then measure the diameter (the distance across the circle through the center). Divide circumference by diameter. You should get a number close to 3.14.
This method is accurate to only two or three decimal places because measuring by hand introduces error. But it shows you directly what pi means: it is always the same ratio, no matter the size of the circle.
The Polygon Method: Archimedes' Approach
Around 250 BCE, Archimedes calculated pi by drawing polygons — shapes with many straight sides — inside and outside a circle. As the number of sides increases, the polygon gets closer to the circle's shape. The perimeter of the polygon approaches the circumference of the circle, and you can use that to narrow down what pi must be.
Start with a hexagon (six sides) inscribed in a circle of radius 1. Calculate its perimeter. Then calculate the perimeter of a hexagon drawn around the outside of the circle. The true circumference (and therefore pi) lies between these two values. Now repeat with an octagon (eight sides), then a 16-sided polygon, then 32 sides. Each time, your upper and lower bounds get closer.
Archimedes used this method to show that pi is between 3.1408 and 3.1429. You can code this in Python in about 20 lines. It converges slowly — you need many polygons to get many decimal places — but it is straightforward to understand and it actually works.
The Leibniz Formula: straightforward but Slow
In the 1600s, Gottfried Leibniz discovered that pi can be calculated from an infinite series:
π/4 = 1 − 1/3 + 1/5 − 1/7 + 1/9 − 1/11 + ...
To find pi, you calculate this sum and multiply by 4. The more terms you add, the closer you get to pi. This is trivial to code: a loop that adds and subtracts fractions with odd denominators.
The problem is that it converges very slowly. To get 10 correct decimal places, you need roughly 10 billion terms. To get 100 decimal places, you need far more. Modern computers can do this, but it takes time. The Leibniz formula is useful for teaching how series work, not for actually calculating pi in practice.
Machin's Formula: Faster Convergence
In 1706, John Machin found a formula that converges much faster:
π/4 = 4 × arctan(1/5) − arctan(1/239)
The arctangent function can itself be expressed as a series. By using two arctangent terms with carefully chosen denominators, Machin's formula reaches accuracy much quicker than Leibniz. You can calculate 100 decimal places in seconds on a modern computer.
Machin's formula (and variations of it) was used to calculate pi by hand in the 1800s and early 1900s. It is still a good choice if you need a few hundred digits and want code that is readable and not too slow.
The Chudnovsky Algorithm: What Computers Use Now
When computers calculate pi to millions or billions of digits, they use the Chudnovsky algorithm, discovered in 1988. It converges so fast that each term gives you roughly 14 new correct digits. The formula is complex and requires high-precision arithmetic libraries, but the payoff is enormous.
The Chudnovsky algorithm is what the y-cruncher program uses when it sets world records for pi calculation. In 2024, pi has been calculated to over 100 trillion digits using this method. You do not need to code it yourself unless you are trying to break a record; Python's mpmath library includes it.
If you want to see how it works, search for "Chudnovsky algorithm" and a programming language you know. The code is dense, but the structure is: compute a sum of terms, each one exponentially more precise than the last, until you have as many digits as you want.
Using Your Programming Language's Built-In Pi
In Python, import math and use math.pi. In Java, use Math.PI. In C, include math.h and use M_PI. These are stored to the precision your computer can handle (usually about 15 to 17 decimal places for a standard floating-point number).
If you need more precision, use the mpmath library in Python. Set the number of decimal places you want with mp.dps = 100, then call mp.pi. The library calculates pi to that precision on the fly.
You only write your own pi calculation if you are learning how algorithms work, if you need more digits than your library provides, or if you want to test your understanding of a specific method.
Testing Your Calculation
Once you have written code to calculate pi, you need to know if it is correct. The first 50 digits of pi are:
3.1415926535897932384626433832795028841971693993751
Calculate your pi to 20 or 30 digits and compare it character by character to this string. If they match, your code is working. If they diverge, you have a bug in your formula or your arithmetic.
For longer calculations, read a file of pi's digits from a reliable source (the Bailey–Borwein–Plouffe formula can calculate individual digits without calculating all the ones before it, which is useful for verification). Compare your output to the known value. If they match to the precision you calculated, you are done.
Frequently Asked Questions
How many decimal places of pi do I actually need?
For engineering and physics, 10 to 15 decimal places is more than enough. For cryptography and most scientific computing, 50 to 100 is standard. Beyond that, you are calculating pi for the sake of it, which is fine — but know that you are not solving a practical problem.
Why does pi have infinitely many digits?
Pi is irrational, meaning it cannot be expressed as a ratio of two whole numbers. Irrational numbers never repeat and never terminate. Mathematicians proved this in the 1700s. You can calculate as many digits as your computer's memory allows, but you will never reach the end.
Which method should I code if I am learning?
Start with the polygon method or Machin's formula. Both are short to code, straightforward to understand, and fast enough that you see results in seconds. Once you understand how they work, you can move to faster algorithms.
Can I calculate pi on a smartphone?
Yes. Python runs on Android and iOS through apps like Pydroid and Pythonista. You can code any of these methods on a phone, though calculating millions of digits will be slow and drain the battery.
What is the world record for pi digits?
As of 2024, pi has been calculated to over 100 trillion digits using the Chudnovsky algorithm and specialized hardware. This took weeks of computation and serves no practical purpose — it is a test of algorithm efficiency and computer performance.