A couple optimizations speeds up the implementation by slightly
under a second for problem problem 3