Improve performance of Java's problem 50.