Let's define a self-contained number as a positive integer, whose digits appear in runs of length equal to themselves only. In other words, any decimal digit d (excluding 0) occurs only in runs of length exactly d.


You can choose any of the three methods listed below:

  • Given an integer n, output the nth (either 0 or 1-indexed) self-contained number.
  • Given an integer n, output the first n self-contained numbers.
  • Print the sequence indefinitely.


  • 133322 is a self-contained number because 3 appears in a run of three 3's, 1 is single and 2 occurs in a run of two 2's.

  • On the other hand, 35553355 isn't, because, although 5 and 3 occur five and three times respectively, they do not form runs of adjacent digits.

  • 44422 is not self-contained, because 4 only occurs three times.

  • 12222333 isn’t either, because 2 appears in a run of four 2's, and it cannot be treated as two separate runs of two 2's.

Not surprisingly, this is OEIS A140057, and its first few terms are:

1, 22, 122, 221, 333, 1221, 1333, 3331, 4444, 13331, 14444, 22122, 22333, 33322, 44441, 55555, 122122, 122333, 133322, 144441, 155555

You can take input and provide output through any of the standard methods, in any programming language, while noting that these loopholes are forbidden by default. This is code golf, so the shortest code in bytes (in every language) wins.


11 Answers 11


Python 2, 104 94 83 bytes

-10 bytes thanks to Mr. Xcoder
-11 bytes thanks to Jonathan Allan

while 1:
 if`i`==''.join(d*int(d)for c,d in zip(`-i`,`i`)if d!=c):print i

Try it online!

  • \$\begingroup\$ ...actually is this acceptable, since it will fall over once i becomes a long? It may be necessary to use str (I'm never really sure of these things though). \$\endgroup\$ – Jonathan Allan Dec 9 '17 at 15:47
  • 1
    \$\begingroup\$ @JonathanAllan it's an interesting question. Normally we are allowed to assume that it's within the standard integer type, not long, but Python doesn't make this distinction very clear... \$\endgroup\$ – FlipTack Dec 9 '17 at 16:10

Mathematica, 66 bytes

Prints the sequence indefinitely


Try it online!

In TIO you have to terminate the execution in order to see the result but in Mathematica works fine.

-12 bytes from Martin Ender


JavaScript (ES6), 76 71 68 bytes

Returns the n-th term of the sequence, 0-indexed.


NB: As always with recursive functions, the input range depends on Tail Call Optimization support and the stack size of your engine.



for(n = 0; n <= 10; n++) {
  console.log('a(' + n + ') = ' + f(n))

Alt. version, 65 bytes

Takes no input and prints the results with alert(), one at a time.


Try it online! (Stops as soon as the maximum stack size is exceeded.)


05AB1E, 9 bytes

Returns the nth term of the sequence, 1-indexed.


Try it online!


µ           # loop over increasing N until counter equals input
 NÔ         # push N with consecutive equal elements deduplicated
   Nγ       # push N grouped into runs of consecutive equal elements
     €g     # get the length of each run
       J    # join to a number
        Q   # check for equality
            # if true, implicitly increment counter
  • \$\begingroup\$ A possible source of inspiration from my 10-byte approach: µNγD€gs€ÙQ \$\endgroup\$ – Mr. Xcoder Dec 9 '17 at 13:05

Jelly, 7 bytes


Try it online!

-4 bytes thanks to Mr. Xcoder's early suggestion.
-1 thanks to Jonathan Allan.

Takes input from STDIN.


Haskell, 70 bytes

import Data.List
filter(all(\s->read[s!!0]==length s).group.show)[1..]
  • 1
    \$\begingroup\$ You can drop the leading x= as per our golfing rules. \$\endgroup\$ – Laikoni Dec 10 '17 at 11:57

JavaScript 4, 83 80 bytes

for(i=0;;)+(++i+'').replace(/(.)\1*/g,function(x,y){return y^x.length})||alert(i)

for(i=0;i<1000;)+(++i+'').replace(/(.)\1*/g,function(x,y){return y^x.length})||alert(i)

  • \$\begingroup\$ "Javascript 1"? Is there such a language name? \$\endgroup\$ – user202729 Dec 9 '17 at 12:35
  • \$\begingroup\$ I mean it works since JavaScript appear, not sure if the name is correwct \$\endgroup\$ – l4m2 Dec 9 '17 at 12:37
  • \$\begingroup\$ sorry it seems not work in js1. I read and found no replace \$\endgroup\$ – l4m2 Dec 9 '17 at 12:39

CJam, 20 bytes


Try it online!


1                       push 1
 {                }h    while TOS is truthy (i.e. forever):            example iteration: 14444
  _                       duplicate                                                       14444 14444       
   Ab                     convert to base 10 (get decimal digits)                         14444 [1 4 4 4 4]
     e`                   run-length encode (array of two-element arrays)                 14444 [[1 1] [4 4]]
       :                  map over the array:
        :                   fold between the two array elements with:
         =                    equality                                                    14444 [1 1]
          :               fold between the array elements with:
           *                multiplication (a.k.a. logical AND for 1 or 0)                14444 1
            {  }&         if this yields a result of 1:
             _              duplicate the number and                                      14444 14444
              p             print it                                                      14444 (output 14444)
                 )        increment the number                                            14445

R, 56 bytes


Try it online!

Makes use of run length encoding on the split number. Returns true if all the lengths equal the values.

Note: I have loaded the methods library in TIO to get el to work.


Pyth, 12


Test online!

  • \$\begingroup\$ 10 bytes: .fqFCr8jZT \$\endgroup\$ – Erik the Outgolfer Dec 9 '17 at 15:18

Perl 6, 49 bytes

{(grep /^((\d)$0*:<?{$/.comb==$0}>)+$/,^Inf)[$_]}

Try it online!

Returns the n-th element of the sequence, zero-indexed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.