Solved (but didn't optimized) and sent Problem 47 to repository.