Solution to Algorithm hw3 problem6