Is this for developing an algorithm (e.g. homework)? Is there a upper limit?

Otherwise, the fastest way would be be to just load a list of the first 10,000 or so numbers and use =MATCH()