I Did Math Homework in Lua and I Regret Nothing

Three tiny Lua programs, one college assignment, and a completely unnecessary amount of enthusiasm for fractals.
So my professor assigned a math-meets-programming project and said we could use any language we wanted. My classmates opened up Python. I opened up Lua.
Was that the practical choice? No. Was it the correct choice? Absolutely yes.
Look — I use Lua for fun. It’s my little scripting comfort food. So when academia said “pick a language,” I said sure, let me do this in the one that makes people go “wait, Lua?” The repo is right here if you want to follow along (or judge me, I get it).
Here’s what I built.
1. The Mandelbrot Set, but make it ASCII
This one is my favorite and I will not be taking questions.
The Mandelbrot set is one of those things where the math is deceptively simple and the output is mind-bending. You take a complex number , you iterate the function:
starting from , and you ask: does this sequence blow up to infinity, or does it stay bounded? The points where it stays bounded form the set. Plot those points and you get the most famous fractal in math.
My version (mandelbrot.lua) maps each character position in the terminal to a complex number, runs the iteration, and uses symbol intensity to show how quickly (or slowly) each point diverges. The output is ASCII art, which means it’s both mathematically rigorous and looks like something from a 1985 BBS.
-- each (col, row) maps to a complex number c = (x, y)
-- iterate z = z^2 + c and count steps until |z| > 2
-- use that count to pick a character: ' ', '.', ':', '+', '*', '#' Honestly, watching this render in the terminal gives me the same serotonin hit as a good CTF flag. Math is just security for the soul.
2. Matrix Addition (yes, the classic)
Okay this one is less visually dramatic but honestly underrated.
matrix_sum.lua adds two 3×3 matrices together. Element-wise, nested loops, clean table indexing. It’s not glamorous but matrix addition is genuinely one of those operations that shows up everywhere — graphics, ML, cryptography, you name it. If you’re in security and you’ve ever looked at lattice-based crypto, matrices are your whole personality now.
The math:
The code does exactly that — walks through both matrices, adds corresponding elements, builds the result. Nothing fancy, but writing it yourself instead of calling numpy.add() is the kind of thing that makes the concept stick.
local function matrix_add(a, b)
local result = {}
for i = 1, #a do
result[i] = {}
for j = 1, #a[i] do
result[i][j] = a[i][j] + b[i][j]
end
end
return result
end 3. Fibonacci: The Recursion Cautionary Tale
fibonacci.lua computes Fibonacci two ways — recursive and iterative — then benchmarks them and compares the call counts. This one is basically a demo of why naive recursion is the villain of algorithm design.
The Fibonacci recurrence is:
Simple, right? The recursive implementation looks exactly like that definition, which is beautifully readable:
local call_count = 0
local function fib_recursive(n)
call_count = call_count + 1
if n <= 1 then return n end
return fib_recursive(n - 1) + fib_recursive(n - 2)
end The problem is that fib_recursive(40) calls itself something like 300 million times. The iterative version does it in 40 steps. Same answer. Wildly different effort.
This is the kind of thing that’s easy to know and still somehow surprising when you actually run it and watch the numbers. The recursive call count grows exponentially — — while the iterative one is . Run both with lua fibonacci.lua 40 and stare at the output for a moment.
Why Lua, though
I get asked this. Here’s my honest answer: Lua is lightweight, fast to prototype in, and has a certain charm to it that I can’t fully explain. It’s the language I reach for when I want to think about a problem without fighting a build system. No CMakeLists.txt, no headers, no linker errors. Just lua script.lua and go.
C++ is my serious language — the one I trust with real work. Lua is more like… a sketchbook. And sometimes a college assignment is the perfect excuse to doodle.
The repo is small on purpose. Three files, one README, zero dependencies. Just Lua and some math. Sometimes that’s enough.
If you want to run any of these, clone the repo and make sure you have Lua installed (lua -v to check). Each script runs standalone — no libraries, no setup.
git clone https://github.com/sarahsec/math
cd math
lua mandelbrot.lua
lua matrix_sum.lua
lua fibonacci.lua 40 The Mandelbrot one especially — run it in a wide terminal window. You’re welcome.