solution to p04 with tail recursion