You can essentially do the same as for very small n, but just make this check more efficient. For example, a naive verification method, if you have already generated a number, is simply a linear search for a list of previously generated values. For unknown n, you can save a set of previously generated values, sorted so that you can use a more efficient search to identify duplicates. With a naive approach, the algorithm takes O (n 2 ) time, but a more reasonable search from previous results can reduce this to O (n * log 2 n).
source
share