Optimization of Problem 5 with least common multiples and greatest common denominators, but I don't get that yet