Given N > 0
and M > 0
, I want to enumerate all (x, y) pairs such that 1 <= x <= N and 1 <= y <= M in descending order of (x * y).
An
This is effectively equivalent to enumerating the prime numbers; the numbers you want are all the numbers that aren't prime (except for all those that have x
or y
equal to 1).
I'm not sure there's a method of enumerating primes that's going to be quicker than what you're already proposing (at least in terms of algorithmic complexity).