: ?DIVISIBLE ( prefix n -- prefix n flag ) \ is prefix divisible by n?
  2dup MOD 0= ;

: _?NEW# ( prefix n -- flag ) \ does prefix contain n?, destructive
  over 0=
  IF     2drop -1
  ELSE   over 10 MOD over - 0=
         IF      2drop 0
         ELSE    swap 10 / swap RECURSE
         THEN
  THEN ;

: ?NEW# ( prefix n -- prefix n flag ) \ does prefix contain n?
  2DUP _?NEW# ;

: DIGITS ( prefix n -- ) \ loop: number starting with prefix and has n digits
  dup 10 =
  IF     drop .
  ELSE   10 1
         DO     over 10 * i ?NEW#
                IF      + over ?DIVISIBLE
                        IF     1+ RECURSE
                        ELSE   2drop
                        THEN
                ELSE    2drop
                THEN
         LOOP
         2drop
  THEN ;

: 9DIGITS ( -- ) \ solve the 9-digits problem
  0 0 1 DIGITS ;

9DIGITS
BYE
