Solution to problem 20 along with a Factorial functions