Algorithm for 'good' number
A give number x is 'good' if the sum of any two consecutive digit of the number x are between k and 2k. I need to find an algorithm that for a given number k and a given number n, find how many 'good' n-digit numbers exist. I made an implementation for this in PHP, but the complexity is to big (i am searching for all those 'good' number and counting them, so the complexity is O(10^n)). <?php $n = 5; $k = 5; $min = $k*1; $max = $k*2; $counter = 0; for ($i = pow(10, $n-1); $i<pow(10,$n); $i++) { $number = $i; $prev = $number % 10; $number = $number / 10; while($number >= 10) { $crnt = $number %