Prolog implementation of Problem_006