Yeah that's a cool exercise and I try to do a lot of these!
I guess expressing it mathematically purely is already done in the code of the original solution, because in a way program == proof. If you have some time, here's a great read on this: http://www.maa.org/sites/default/files/pdf/upload_library/22...