22
\$\begingroup\$

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.

Task

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.

Examples

  • 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.

\$\endgroup\$

15 Answers 15

8
\$\begingroup\$

Python 2, 104 94 83 bytes

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

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

Try it online!

\$\endgroup\$
  • \$\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
6
\$\begingroup\$

Mathematica, 66 bytes

Prints the sequence indefinitely

Do[##&&Print@t&@@(#==Tr[1^{##}]&@@@Split@IntegerDigits@t),{t,∞}]

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

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 9 bytes

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

µNÔNγ€gJQ

Try it online!

Explanation

µ           # 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
\$\endgroup\$
  • \$\begingroup\$ A possible source of inspiration from my 10-byte approach: µNγD€gs€ÙQ \$\endgroup\$ – Mr. Xcoder Dec 9 '17 at 13:05
6
\$\begingroup\$

JavaScript (ES6), 76 71 68 bytes

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

f=(n,k)=>+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||n--?f(n,-~k):k

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

Demo

f=(n,k)=>+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||n--?f(n,-~k):k

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.

f=k=>f(-~k,+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||alert(k))

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

\$\endgroup\$
4
\$\begingroup\$

Jelly, 7 bytes

DŒrZEµ#

Try it online!

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

Takes input from STDIN.

\$\endgroup\$
2
\$\begingroup\$

CJam, 20 bytes

1{_Abe`::=:*{_p}&)}h

Try it online!

Explanation:

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
\$\endgroup\$
2
\$\begingroup\$

Haskell, 70 bytes

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

Brachylog, 10 bytes

≜ℕẹḅ⟨l=h⟩ᵐ

Try it online!

Infinitely generates elements of the sequence through its input variable. (If it actually has to do the printing itself, append &ẉ⊥.) This is essentially code to solve the corresponding with a prepended to brute-force the smallest solutions first:

        ᵐ    For every
  ḅ          run of
 ẹ           digits in
             the input variable
ℕ            (which is a non-negative integer),
   ⟨l  ⟩     its length
   ⟨  h⟩     and its first element
   ⟨ = ⟩     are equal.

I expected this to only take 9 bytes, but seems to require an explicit to separate the digits of a number into runs.

\$\endgroup\$
1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\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
1
\$\begingroup\$

Perl 6, 49 bytes

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

Try it online!

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

\$\endgroup\$
  • \$\begingroup\$ The \d can just be . \$\endgroup\$ – Jo King May 27 at 3:22
1
\$\begingroup\$

R, 56 bytes

function(n)all((r=rle(el(strsplit(c(n,''),''))))$l==r$v)

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.

\$\endgroup\$
1
\$\begingroup\$

Stax, 10 bytes

Ç≡∟Öz≈¢αV¢

Run and debug it

This program filters all positive integers with a filter. The digits are run-length encoded. For each run, the digit must equal the run length.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 12

.f.AqMr8jZ10

Test online!

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

Perl 5 -p, 48 bytes

++$\=~s|(.)\1*|$1-length$&&&redo|gre for 1..$_}{

Try it online!

Returns the n-th element, 1 indexed.

\$\endgroup\$
0
\$\begingroup\$

Java 10, 121 bytes

A lambda from int to int. The function takes an index n and returns the nth (1-indexed) sequence value.

n->{int x=0,m=1;for(;n>0;n-=m,m=1)for(var p:(++x+"").split("(?<=(.))(?!\\1)"))m=p.length()==p.charAt(0)-48?m:0;return x;}

Try It Online

Ungolfed

n -> {
    int x = 0, m = 1;
    for (; n > 0; n -= m, m = 1)
        for (var p : (++x + "").split("(?<=(.))(?!\\1)"))
            m = p.length() == p.charAt(0) - 48 ? m : 0;
    return x;
}
\$\endgroup\$

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.