This is at least my ninth year of writing about Pi Day—here is my post from 2010. Of course it’s called Pi Day because the date, 3/14, is similar to the first three digits of pi (3.1415 …). At this point I’ve built up a whole library of fun things in honor of Pi Day.

Here is a new one. You can calculate the digits of pi using elastic collisions between two objects of different masses and a wall. Let me explain with this diagram.

There are two balls, A and B. Ball A has a larger mass and is initially moving. It collides with ball B such that ball B speeds up and ball A slows down just a little bit (this is a perfectly elastic collision). After this, ball B starts moving toward the wall and eventually bounces off it back toward ball A for another collision. This continues until ball A is moving away from the wall instead of toward it, and there are no longer any collisions.

Now for the pi part. If you know that the mass of ball A is 100 times greater than that of ball B, there will be 31 collisions. If the ratio of masses is 10,000 to 1, there will be 314 collisions. Yes, that is the first 3 digits of pi. If you had a mass ratio of 1 million to 1, you would get 3,141 collisions. (Remember the first few digits of pi are 3.1415 …) In general, if you want “d” digits of pi, then you need mass A divided by mass B to be 100 raised to the d-1 power.

This is not a very efficient method for calculating the digits of pi, but it seems to work. Here is a great video from 3Brown1Blue that explains this situation. Also, here is an older video from Numberphile that also goes over this problem.

This is crazy awesome. I don’t even understand how it works. But that’s not why I’m here. Instead, I am going to show you how to model this phenomenon with a numerical calculation. It’s going to be fun.

I guess the first thing to address is: What the heck is an elastic collision? There are really two things to consider in a collision. There is the momentum of the objects, where momentum is the product of mass and velocity. If there are no external forces on the two objects colliding (or the collision happens over a very short time frame), the total vector momentum of the objects before the collision is equal to the momentum after the collision. We call this conservation of momentum.

The other quantity to consider in a collision is the kinetic energy. Like momentum, this also depends on the mass and velocity of the object. But there are two important differences. First, the kinetic energy is proportional to the product of mass and the velocity squared. Second, momentum is a vector and thus has direction, but kinetic energy is a scalar with no direction.

In most collisions, momentum is conserved but kinetic energy is not. However, in special collisions called elastic collisions, both momentum and kinetic energy are conserved. These are the collisions we need to calculate pi.

Although it is indeed possible to use momentum and kinetic energy to find out how many times two balls collide, I’m not going to do that. Instead, I am going to do this as a numerical model. In a numerical model, you make some basic calculations and then just break the problem into a bunch of tiny steps. In this case, the tiny steps will be short time intervals over which I assume stuff is constant. Trust me, this works.

But how do you model a collision? One way is to pretend that the balls have springs inside them (which isn’t completely wrong). If the radius of two balls overlap, there will be a spring force pushing them apart. The magnitude of this spring force is proportional to the amount the two objects overlap. Because this spring force will be the only force acting on the two objects, momentum will be conserved. And because the energy stored in the spring has no energy losses, kinetic energy will also be conserved. It’s a perfectly elastic collision.

What about the collision with the wall? In that case, it’s just like the collision between two balls but with one difference. I don’t let the wall change its position or momentum—you know … because it’s a wall.

Now for the numerical calculation. Here is a collision between two balls with a 100 mass ratio. If you want to run it again, just click the Play button. If you want to see and edit the code, click the Pencil.

It works. There are 31 collisions in this model—which are the first two digits of pi. What if you want three digits? You can try changing the masses, but it doesn’t work. The problem is that when the large mass gets very close to the wall with the small mass in between them, things don’t happen the way you intend. You can actually get the small mass interacting with both the wall and the big mass at the same time. Although this is realistic, it doesn’t give us the best calculation of pi.

So, how do you fix it? I have a couple of options (and you can try this as your homework assignment). The first method would be to fix this numerical spring-based model. I think that if you change the time step (dt) and the spring constant (k) as the balls collide, you can get a better answer. Here’s what you would do. As the balls get closer together, make a smaller time step and a larger spring constant. This would make the ball–ball collision more accurate in the cases where the smaller ball is getting crushed.

The next option is to just abandon the spring-based collision model. Instead, you could calculate the velocities of the balls analytically after each collision. Surprisingly, a one-dimensional, perfectly elastic collision isn’t such a simple problem to solve. But don’t worry, I did it for you and covered all the details. I even made a python function that takes the two objects with the starting velocities and returns the velocities after the collision. Yes, I really gave you a head start on this last problem. Maybe I will save this for next year’s Pi Day.

### More Great WIRED Stories

- There’s a way to keep parents from fleeing STEM careers
- The NSA open sourced a powerful cybersecurity tool
- Amazon’s algorithms have curated a dystopian bookstore
- How Arrivo got Colorado to back this highway scheme
- Boss acting nicer recently? You may have VR to thank
- 👀 Looking for the latest gadgets? Check out our latest buying guides and best deals all year round
- 📩 Hungry for even more deep dives on your next favorite topic? Sign up for the Backchannel newsletter