Since I like things based on tables, I would first generate a table for n = 2 (<100 records, obviously) and just keep it in an initialized array.
Then f (n) = digits in the sequence N [1, 2, 3, 4, 5, 6, 7, 8, 9], composed with f (n-1), where f (n-1) [0]> N
ie for n = 3: 1, f(2) where f(2)[0] > 1: 123, 124, 125, ... 2, f(2) where f(2)[0] > 2: 234, 235, 236, ... ...
source share