This was an interview question, which seems related to Project Euler Problem 14
Collatz conjecture says that if you do the following
If n is even, replace n by n/2.
If n is odd, replace n by 3n+1.
You ultimately end up with 1.
For instance, 5 -> 16 -> 8 -> 4 -> 2 -> 1
Assuming the conjecture is true, each number has a chain length: The number of steps required to get to 1. (The chain length of 1 is 0).
Now, the problem is given natural numbers n, m and a natural number k, give an algorithm to find all numbers between 1 and n, such that the chain length of those numbers is <= k. There is also the restriction that the chain of any of those numbers must only include numbers between 1 and m (i.e. you cannot go over m).
An easy way is to brute-force it out, and combine that with memoization.
The interviewer said there was an O(S) time algorithm, where S is the number of numbers we need to output.
Does anyone know what it could be?