Solution to the ugly numbers problem.