Rolando Andrade
Back to projects

Built in 2023

PiDay 2023

A Thousand Digits of PI

PiDay 2023 preview

Calculation of the digits of the PI number with the Chudnovsky algorithm, shown as a grid of colored tiles.

A thousand digits of pi, each one in its own colored square. Every 1 is pink, every 5 is teal, every 9 is brown. The page fills your screen with them, and a counter at the top says “1000 Digits of PI”.

My earlier Pi Day projects were about watching pi slowly appear. Random darts gave about two correct decimals. A series of fractions crept closer, step by step. Pi Day 2023 goes the other way. It shows real digits, many of them, calculated in the browser.

That needs a much faster formula. I used the Chudnovsky algorithm, one of the fastest ways to calculate pi that we know.

A wall of colored digits

The page has a gray header that stays on top while you scroll. Below it, a grid shows a “3”, a decimal point, and then the digits of pi, one per tile. Each digit from 0 to 9 has its own background color. A small number in the corner of every tile tells you its position after the decimal point.

At the end of the grid there is a tile with a plus sign. Click it, and one more digit appears. The counter in the header goes up by one. You can keep clicking as long as you like.

PI Day 2023 lab.rolandoandrade.me

Fourteen digits per step

The brothers David and Gregory Chudnovsky published their formula in 1988. It is an infinite series, like the Leibniz series in my Pi Day 2020 project. The difference is speed. Leibniz needs hundreds of terms to get a few digits right. Chudnovsky adds about 14 correct digits with every single term.

The code even stores that rate as a constant, 14.18 digits per iteration. To get 1000 digits, it divides by 14.18 and knows how many terms it needs.

The formula uses a few big fixed numbers. You can see them at the top of the code:

  1. 13591409 and 545140134, which build the top part of each term.
  2. 640320, whose cube appears in the bottom part.
  3. 426880 and the square root of 10005, which appear in the final step.

With all the terms added, the last line divides one big number by another, and pi comes out.

Numbers bigger than JavaScript

Normal JavaScript numbers hold only about 16 significant digits. That is not even close to 1000. So the project uses bignumber.js, a library for numbers of any size and precision.

Before the calculation starts, the code tells the library how many decimal places to keep. It uses the number of digits times the base 2 logarithm of 10. That converts decimal digits into bits, so the library keeps enough precision for the whole result.

Splitting the work

Adding the terms one by one would be slow, because each term involves huge factorials. The code uses a method called binary splitting instead. It cuts the range of terms in half, solves each half, and joins the two results. Each half is split again, and again, until only one term is left.

Every piece returns three values, called P, Q and T. Joining two pieces needs only a few multiplications and one addition. At the end, Q and T go into the final formula, together with the fixed constants.

There is one more trick. Each half is calculated inside a setTimeout. This gives the browser short breaks to stay responsive while the math runs, instead of freezing the page.

Calculating ahead

The page shows 1000 digits, but it asks for twice that number on the first run. It keeps the extra digits in memory.

When you click the plus tile, the code first checks this saved result. If there are enough digits left, it shows the next one right away. Only when the saved digits run out does it calculate again, and again it asks for double. So most clicks cost nothing at all.

Credit and tools

The Chudnovsky implementation is not my own. The algorithm and its code come from the Gerpea/pi repository, and the README says so. My part was the page around it: the colored grid, the counter, the plus button and the caching of extra digits.

The project is written in TypeScript and built with Vite, a fast build tool for web projects. A small deploy script builds the site and pushes the result to a separate deploy branch.

From darts to digits

Looking at all my Pi Day projects together tells a small story. In 2018 I threw random darts. In 2019 I counted collisions between two balls. In 2020 I drew a slow series on a graph. In 2023 I asked the browser for a thousand digits.

Each method shows something different about the same number. This one shows how far the math has come.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build