← Back to Blog

Leibniz's Marble Computer

In reading The Clockwork Universe one of the standout sections was hearing about Leibniz's idea for a marble computing machine. This is fascinating from a historical context, but also because it's awesome to think about non silicon based computers. I read The Three-Body Problem a few years ago and one of the sections that stuck with me is the alien race discovering computing through an army flag based system. For today's post, I decided to walk through creating a marble based computation machine. Inspired by Leibniz and the Three-Body Problem, but taking it in any direction that feels fun to me.

Some quick history first. In March of 1679 Leibniz wrote a three page manuscript on binary arithmetic, De Progressione Dyadica, working out how to add, subtract, multiply and divide with nothing but 0s and 1s. Near the end he describes a machine, a box with a row of holes across the top, each one open for a 1 or shut for a 0. Pour marbles over it and they fall through the open holes into channels below. He sketched how it would add, and how sliding the row of holes sideways would let it multiply. Then he shelved it and went back to his gear driven decimal calculator, which is a little sad, because that calculator's carry mechanism never worked properly and this one was a box with holes and a handful of marbles. Nobody built the marble one until 1971, when the Deutsches Museum in Munich made a wooden model, and a working replica followed in 2003 at Leibniz University Hannover.

One rule

The whole machine comes down to one rule, so the first demo is that rule and nothing else. Below is a row of four channels, and each channel holds at most one marble. A marble in a channel is a 1 and an empty channel is a 0. The ones place is on the right, then twos, fours and eights running left, so you can read the row as a four bit binary number. The arrows at the top drop a marble into whichever channel you pick, and the +1 button drops one into the ones channel.

When a marble lands in a channel that already has one, the two collide. One falls out the bottom and is gone, and the other moves one channel to the left.

Count up with +1 and keep going past 15. At that point every channel is full, so one more marble sets off a chain of collisions that runs all the way across the rack and empties it. I find this weirdly satisfying to watch, and it is the same thing your CPU does every time a counter rolls over, just slow enough to see.

Leibniz's channel rule. A resting marble is a 1. When two marbles share a channel, one falls out and one passes left.

The addition machine

The next demo adds the part of Leibniz's design that does the real work, the row of holes. Leibniz described one sliding row, and the demo gives you two so you can see both numbers go in. There are two rows of gates above the channels, one for A and one for B, and each gate is open for a 1 or closed for a 0. You can set A and B by typing numbers into the boxes or by clicking the gates, and the two stay in sync. When you release the marbles, one falls through every open gate in A's row and lands in the channel below it, then the same happens for B's row. Any channel that ends up with two marbles resolves the collision the same way as before, so the carries take care of themselves. The channels end up holding the sum, and the readout shows it in binary and decimal.

There are five channels, so anything past 31 carries off the left edge and is lost. Try 20 + 20 and the machine reports 8 with a straight face. That is integer overflow, designed in 1679, about three centuries before anyone stayed up late wondering why a counter wrapped to zero. Slow motion is worth turning on at least once for something like 15 + 1.

Set A and B by typing or by toggling the gates, then release.

Multiplying by sliding the gates

The multiplier is the trick from the manuscript that I think gets overlooked. Leibniz writes that the row of holes "is to be shifted from column to column as required," and that one line is the whole idea. Shift the gates one place to the left and every marble lands in a channel worth twice as much, so a shifted release drops a doubled copy of the number into the channels.

In the demo the gate row holds a, and b is shown as a row of bits along the top. The machine walks through the bits of b from lowest to highest. For each bit that is a 1, it slides the gate row that many places to the left and releases the marbles, and the channels add each copy onto whatever is already there. Each release gets its own color so you can see the partial products stacking up. Once the last bit of b is done the channels hold the product.

This is shift-and-add multiplication, which is roughly what the multiplier circuit in your processor does. He got there in one sentence and then put the pages away.

One shifted release per 1-bit of b. The channels accumulate the product.

The marble computer

The last demo turns the racks into a programmable computer. There are four racks now, named A, B, C and T, and each one holds a number the same way the channels did above. Instead of gates there is a program, a list of instructions the machine works through one line at a time. The whole instruction set is five moves:

  • INC r drops one marble into rack r
  • DEC r takes one marble out of rack r
  • JZ r label looks at rack r, and if it is empty, jumps to label
  • JMP label jumps to label
  • HALT stops

The three presets each load a program and starting values for the racks. Countdown drains A one marble at a time. Move A into B does what it says, and that loop, take one out of here and put one in there until here is empty, is the basic move everything else is built from. Multiply computes A times B into C without a multiply instruction, by adding B into C over and over and using T to hold onto B between passes. This is the preset that convinced me the thing is a computer and not a toy. 3 × 4 takes 63 marble moves and you can watch every one of them.

Run plays the program through. Step runs one instruction per click, so you can watch the highlighted line and the marbles move together, and the speed buttons change how fast the marbles fall. Edit opens the program as text, and the boxes above each rack set its starting value.

DEC is the instruction to watch. If the lowest marble in a rack is sitting in the fours channel, the machine cannot just take one away. It has to break the four into a two and a one first and then remove the one, which is borrowing, the thing you did on paper in second grade, done with marbles. If you write a program that never halts, the machine gives up after 500 steps, which I know because I wrote several.

INC drops a marble, DEC borrows one out, JZ jumps when a rack is empty. Five instructions and, given big enough racks, Turing complete.

Bonus: the soldier computer

The other computer I mentioned at the top is the one from The Three-Body Problem, where Von Neumann borrows an emperor's army and turns the soldiers into a computer. It runs on a different idea from the marbles. Every soldier holds a flag, black for 1 and white for 0, and three soldiers make a gate. Two of them are inputs. The third watches those two and raises a flag by a rule Von Neumann gave him, something like "raise black only if both of the soldiers you watch raise black." That rule is an AND gate, and with a handful of other rules you get the rest of the gates a real processor is made of.

The demo starts with a single gate. Pick a rule, click the two soldiers in the front rank to flip their flags, and watch the third soldier respond. Switching to the adder wires seven gate soldiers together into a two bit adder. Flip the front rank to set the two numbers and the change sweeps down through the ranks one row at a time, which is the wave of flags the book describes rolling across the formation. In the book it takes thirty million soldiers to build a whole processor this way, standing in a square six kilometers on a side. Eleven is enough to add two 2-bit numbers, which gives you some idea of the ratio.

The marble computer keeps numbers as piles and computes by moving one marble at a time. The soldiers keep numbers as patterns of flags and settle the whole answer in a few beats. The second way is much closer to how the chip in your machine does it, and I think the two demos side by side explain more about computers than either does alone.

The human formation from The Three-Body Problem. Black flag is 1, white is 0. Flip the front rank and watch the wave move through the ranks.