maybe it helps if you have a look into the width of numbers: the numbers from 0:9 have size ceil(log10(number))=1, after that the numbers from 10:99 have width ceil(log10(numbers))=2 and so on. this way you could have a look only on the numbers in the decade you are interested in.
by the way, how big gets your n? you can easily use smth like
sprintf('%i',1:10000)
and then extract the numbers you are interested in?