inelegant solution to problem 4, but it's fast enoguh to pass