First 4 exercises (algorithm 1)