Last week, I posed a Puzzler
. I solved it the hard way, because I did not know any of the fancy math that the people in the comments section mentioned. I will leave the closed form solution to them and the cited references.
The monte carlo solution that I came up with gave a great opportunity for code optimization via the profiler. Here is a six minute video that shows the four iterations my code took. I ended up restructuring so that I did not have clarifying “do nothing” code. I got rid of a clarifying subfunction, and I stopped keeping track of all the iterations, and just stored the final result in an accumulator.
These optimizations cut the execution time by a factor of ten.