"Pi Day" Lua Shenanigans

This year’s “Pi Day” (3/14) is approaching, so I crafted a way for an autopilot to participate…

Here’s less than 100 lines of Lua (3.1415kb) that will calculate Pi to 500 digits (499 decimal places) on a capable autopilot, output to the SD card as pi.txt. Set SCR_HEAP_SIZE to 204800 or more to take full advantage of the implementation.

pi-day.lua (3.1 KB)

Initially, I thought it’d be fun to showcase the scripting environment with a script that would incrementally/iteratively calculate Pi during an auto mission, but the spigot algorithm I chose turned out to be a bit of a memory hog, so the resulting script just starts crunching numbers 5 seconds after boot time and then exits when complete. I didn’t want to risk someone experiencing an OS panic/crash while airborne!

I tested on a Matek H743-WLITE - the least fully featured, scripting enabled autopilot in my collection. Results were compared against those found here.

If someone can beat my 500 digits by an order of magnitude or more, I’d love to see the method. I’m sure it’s entirely possible!


I’ve got to hurry up and get my mower going so it can do really useful stuff like this!! :rofl:

Good Lua lesson and Pi Day application, though!

1 Like

Thanks, Kenny!

Perhaps the biggest lesson learned here: if you have to spread calculations across multiple time/execution slices AND scale your implementation to available memory, perhaps you should seek an alternate solution!

But, a corollary:
If ever you feel constrained by floating point precision, consider that the demonstrated algorithm here calculated 500 digits of Pi using purely integer arithmetic on a processor whose largest unsigned single value is 4,294,967,295 (or less than half of all integer values consisting of 10 decimal digits).

1 Like