updated solution for problem 4. Used while loop and avoided some calculations