MS Excel is Turing complete

This is a random programming post I’m making because of this discovery I’ve just made. And no, it’s not a meme this time.

So, I think most of us have used Excel at some point, either to make some calculations that’d be too cumbersome to do by hand, or driven by our endless love to spreadsheets. And so, most of us may or may not have used a formula - that little function that starts with an equals sign that does some computation - be it summing a range, finding an average, or performing some statistical calculations.

Before I begin my explanation, I just want to note that this works only in Excel 365 standalone, it doesn’t work in the web version sadly.

This is about one specific formula called LAMBDA. This formula creates a callable function with given parameters. So, a thing like

would create a callable function that takes two parameters - x and y - and return the sum of the two.

This formula formally makes MS Excel a functional programming language, which is Turing complete. What does it mean exactly? Let me explain:

If an thing is Turing complete, it means that the aforementioned an thing can calculate anything that is computable. That is, you can create a Turing machine that would perform the exact same calculation. A Turing machine is a theoretical machine consisting of a read/write “head”, an infinitely long tape with cells, an internal state and a ruleset explaining what to do in a given situation: for example, if the head reads 0 and the machine is in state A, write 1 in that cell, move the head to the right, and change its state to B.

Also, a random side note, there exists a Turing machine that can run Minecraft.

In lambda calculus, we operate only on functions. And I mean functions in a very mathematical sense: as a another thing that takes some values and spits out another value. I’ll link to this article explaining the lambda calculus for curious readers, as I’m not really that knowledgable (yet) on this topic. The most important thing is that it can simulate the Turing machine, and as a consequence, we can express any computation using only lambda calculus.

This LAMBDA function in Excel fits perfectly in the definition of a function in lambda calculus, as in, it defines its inputs and its output. And that’s it. We don’t tell how to do what it’s meant to do step by step, we just tell it “hey, take those numbers and do that thing with them”.

This whole thing means that we can write any program using only Excel formulae. No macros, no scripts, just pure Excel. And it’s such a fascinating thing, because that means that Excel is the most successful programming language in history.

Now, the one thing that was impossible without macros was recursion. A recursive function calls itself. Take a look at this implementation of the Fibonacci number algorithm:

We can see that if x is 0, we return 0. If x is 1, we return 1. But if x is greater than 1, we sum whatever result is for x - 1 and x - 2.

So let’s say that x = 5. Then:

  • Fib(5) = Fib(4) + Fib(3)

  • Fib(4) = Fib(3) + Fib(2)

  • Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1

  • Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1

So, going from the bottom:

  • Fib(3) = Fib(2) + 1 = 1 + 1 = 2

  • Fib(4) = Fib(3) + Fib(2) = 2 + 1 = 3

  • Fib(5) = Fib(4) + Fib(3) = 3 + 2 = 5

By recursively calling the Fib function from itself, we’ve managed to calculate the Fibonacci number using the above formula. Before it was not possible to do it using just one function like this. But it wasn’t completely impossible in this example - you could still perform this calculation using the spreadsheet itself, like below:

It’s just that the LAMBDA formula gives you this freedom:

And better yet, you could put there any number expression! You could use certain cells of the spreadsheet as memory and use that to write a fully working program! (You can use just one column to store 1,048,576 different values! In theory, it gives you 8 MB of memory per column, given that every value is 64-bit. Alternatively, you could write a function that limits a number to 32 bits, meaning that with 8 columns, you could in theory emulate the RAM of PlayStation 2 console, which is kinda nuts. The problem arises when we want to write data there, but that’s for another day.)

ok that’s the end of my micro-obsession around excel formulae you are now free to go