Solution for problem 1.6.1
Final submission after correcting for input/output errors.