Implementation for Problem 4.