Instead of searching all numbers and checking them one by one, why not construct the numbers from the rule. In other words, start with the smallest digit being 1, then the next digit being 2 and so on. The digits can then be increased right to left as long as the rule holds. For example, finding all 8 digit numbers: Start with each digit increasing by 1 left to right: * 12345678 Add one to the final digit: * 1234567**9** Run out of digits so increase the digit to the left: * 123456**8**9 * 12345**7**89 * 1234**6**789 * ... * **2**3456789 EDIT for further explanation: For smaller numbers, you then need to repeat this process recursively. For example: * 123 -> 124 -> 125 ... 129 You would now need to "reset" the second digit and any following it to one above what it was: * 129 -> 134 -> 135 -> 136 ... 139 * 139 -> 145 -> 146 -> 147 ... 149 * 149 -> 156 -> 157 -> 158 -> 159 And so on. You are still effectively working backwards through the digits, recursively increasing them right to left. Hope that helps.