Built in 2019
PiDay 2019
PI from Colliding Balls
Calculation of the PI number by counting the collisions between two balls with proportional masses and a wall.
Made with
A small ball rests near a wall. A huge ball rolls toward it, very slowly. They hit. The small ball flies back, hits the wall, and returns to hit the big ball again. Back and forth, faster and faster. Count every hit, and the final number is 31415.
Those are the first five digits of pi. Nobody wrote pi into the code. No circle appears anywhere on the screen. The digits come out of two balls, one wall and the laws of motion.
The mathematician Gregory Galperin described this puzzle in 2003. For Pi Day 2019, I built a simulation of it in the browser. You can watch the counter climb and stop on pi.
Two balls and a wall
The page shows a 400 by 400 pixel canvas with a dark background. On the left there is an invisible wall. Near it sits a small purple ball. To the right is a larger orange ball.
The orange ball starts moving left. As soon as the balls touch, a counter appears below them in large white numbers. The count rises quickly while the small ball rattles between the wall and the big ball. Then the big ball turns around and leaves. The counter stops.
PI The masses decide the digits
The secret is the mass ratio. The small ball has a mass of 1. The big ball has a mass of 100 to the power of 4, which is 100,000,000.
The rule works like this:
- If both masses are equal, you get 3 collisions.
- If the big ball is 100 times heavier, you get 31 collisions.
- If it is 10,000 times heavier, you get 314 collisions.
- Each extra factor of 100 adds one more digit of pi.
In the code, a variable called accuracy controls the exponent. It is set to 4, so the simulation should give five digits. It does. The counter stops at 31415, and the page shows it as “3,1415”, with a comma as the decimal mark, the way it is written in Spanish.
Perfect bounces
Every collision in the simulation is elastic. That means no energy is lost. Real balls get warm and make noise when they hit. These balls do not.
Two laws decide what happens after each hit. The total momentum (mass times speed) stays the same. The total kinetic energy (the energy of motion) also stays the same. From those two laws you get two formulas, one for each ball’s new speed. The bounce method in the code applies them every time the balls touch.
The wall is simpler. When the small ball reaches it, its speed flips sign. It moves right instead of left, at the same speed. That also counts as a collision.
Why pi appears
The reason is geometry. Here is the idea in simple steps.
Picture a graph where one axis is the speed of the small ball and the other is the speed of the big ball. Scale each axis by the square root of the ball’s mass. On that graph, the energy rule says every possible state sits on a circle.
Each collision moves the point to a new place on that circle. Every pair of hits rotates the point by the same small angle. That angle depends only on the mass ratio. The bouncing ends when the big ball moves away faster than the small one can follow. So the number of hits is about how many small angles fit inside half a circle. Half a circle is pi, measured in radians. That is where pi hides.
Tiny steps in time
The physics is simple, but the timing is tricky. The big ball is so heavy that the small ball can reach enormous speeds. If the balls moved too far in one step, they could pass through each other. The simulation would miss a hit, and the count would be wrong.
So the code takes very small steps. The big ball starts with a speed of 1/1000 pixel per step. Each frame runs 1000 steps of movement and collision checks. A timer starts a new frame every 2 milliseconds. Only after all 1000 steps does the page draw the balls and the counter.
There is one more detail in the drawing. The small ball can end up a little past the wall or overlapping the big ball between steps. The draw method fixes the positions on screen only, so the balls always look like they are touching cleanly. The physics keeps the true values.
Plain JavaScript again
The project has three small script files. One holds simple shapes, a rectangle and a circle. One holds the Ball class with its movement, collision and bounce logic. The last one sets up the scene and runs the loop. There is no library and no build step.
The year before, I estimated pi by throwing random darts at a circle in Pi Day 2018. That method gives a different answer each time. This one gives exactly 31415 every time, because physics has no randomness here.
Two balls, a wall and a counter. That was enough to find pi.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build