Problem 5

The most refined version of Problem 5 I was able to come up with, which
takes about 2.5 seconds on my PC.