solution (slow) to problem 78