problem1 solution with a functional approach