Skip to main content
Added explanations from comment request.
Source Link
QuantumChris
  • 1.4k
  • 6
  • 16

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:

  • 12345679

Run out of digits so increase the digit to the left:

  • 12345689
  • 12345789
  • 12346789
  • ...
  • 23456789

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.

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:

  • 12345679

Run out of digits so increase the digit to the left:

  • 12345689
  • 12345789
  • 12346789
  • ...
  • 23456789

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:

  • 12345679

Run out of digits so increase the digit to the left:

  • 12345689
  • 12345789
  • 12346789
  • ...
  • 23456789

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.

Source Link
QuantumChris
  • 1.4k
  • 6
  • 16

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:

  • 12345679

Run out of digits so increase the digit to the left:

  • 12345689
  • 12345789
  • 12346789
  • ...
  • 23456789