This is not the most efficient algorithm for the problem. It can compute only up to 10^7 in a minute.