TITLE: First $k$ numbers with given prime factors
QUESTION [6 upvotes]: Please help me with the following question. 
There are three prime numbers given $p_1, p_2$ and $p_3$. Print the first $k$ numbers with only these prime numbers as factors. 
Expected solution complexity: $O(k)$
For example: the three prime numbers are $2, 3, 7$.
First $7$ numbers with these as factors are $2, 3, 4, 6, 7, 8, 9$.

REPLY [2 votes]: I think the following algorithm will work:

Guess an upper bound $u$ for the numbers you're looking for. Your initial guess is some fixed number $\ge 2$. (For simplicity, just say it is $2$).
Calculate all multiples of your three primes that are smaller than $u$, but not necessarily in increasing order. This can be done in time linear in the number of multiples you find, by a depth-first search in an (implicit) tree where each multiple appears once -- say, remember for each number you've produced what the largest prime factor of it is, and then consider its children to be its product with that and any larger primes.
If you find fewer than $k$ multiples in step 2, your $u$ was too small. Set $u\leftarrow u^2$ and try again.
Use a linear selection algorithm to find the $k$th smallest among the products you computed in step 2.
Output the numbers in the list that are less than or equal to the one you just found. (They won't be in increasing order, but nobody asked for that!)

Why does this run in linear time?
Each time you square $u$, roughly eight times as many multiples will be found in the next round. To see this, consider the task of finding the $k$ smallest integer combinations of $\log p_1$, $\log p_2$ and $\log p_3$. This amounts to cutting a tetrahedral corner off the three-dimensional integer lattice $\mathbb N^3$, by a plane whose orientation depends on the ratio between the logarithms. Squaring $u$ corresponds to doubling the side lengths in the tetrahedron, so $8$ times as many lattice points will be in the corner we cut off next.
Therefore the last round will find at most $8k$ numbers, give or take a few.
And the total effort made in all rounds other than the last will be dwarfed by the last round anyway: By familiar properties of geometric series, they will add up to only a seventh of the last round.

Actually, you may get away with less work than that, if in each round you keep the results of earlier rounds and just continue the search from the points you discarded as too large in the previous round (which you would also remember). Then you don't have to square $u$ for each round, as long as it does grow fast enough that each discarded point will be accepted for the next round (so we can amortize the cost of discarding and reconsidering it). This would correspond to $u\leftarrow p_3u$ instead of $u\leftarrow u^2$, like Marcelo Fornet suggests.
The downside of this is that for fixed $k$, $p_1$ and $p_2$, the work we need to spend will then potentially increase when $p_3$ is chosen larger.
To get the best of both worlds we could instead set $u\leftarrow\min(p_3u,u^2)$.