Solution to the 6th problem using R