You’ve probably seen a magnetic sculpture toy before: a magnetic base with balls or other small metal pieces that can be crafted into some sort of “art” to keep your hands busy.

On a recent visit home, I wound up creating a pyramid with one of these things. Once it had been nicely ordered, I casually wondered how many balls it contained and then started thinking it through out loud. My dad (who teaches math and holds a degree in math) and my brother (a front-end web developer who also holds a math degree) were intrigued. Everyone else—not so much.

It turns out that this is actually a fairly challenging problem if you want to derive a formula to find the number of balls *b* for a triangular pyramid of a given base length *n*. Hint: the standard way to find pyramid volume—the area of the base times the height divided by three—won’t cut it.

What makes this notable enough to write about here is the approach my brother took pretty quickly:

“I don’t know, but I could easily write a program that will perform the calculation for any given value of n in less time than it would take me to derive a formula...”

He is, of course, talking about writing a program that can simply loop through this formula for each value of n minus one down to zero, sum them all together, and spit out an answer.

The interesting part here is that his solution isn’t what I would call elegant—that is, reduced to a simple form—but it leverages technology to arrive at the answer faster and is thus more efficient. So in a short-term business sense, that’s a win, and it will work fine for the purpose of calculating the number of balls in a sculpture I could build by hand.

The twist comes when you try to scale up this kind of process. The coded solution might be great for solving the ball-volume problem for a few moderately sized pyramids, but what happens if I want to start calculating the ball-volume of pyramids that are thousands or millions of balls wide? That’s a lot of loops to perform, and this type of design (fast in the short term) might run into trouble when scaled up a few orders of magnitude. You’d be much better off spending the time to get a simple formula first.

The part that I find the most interesting is that while it’s nice to strive for elegance, it’s not always possible or practical to do so. Figuring out where to use “fast and good enough” rather than “best” is an ongoing challenge that’s very context-specific.

**This simplifies to**

**b = (n³+3n²+2n)/6**

The first few layers (1, 2, 3, 4) are easy enough to verify in your head, and when I plug in 9 (the actual base of the pyramid I created) the math tells me that it’s comprised of 165 sculpture balls. Neat.