The sequence of self-contained numbers

22

2

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.

Mr. Xcoder

Posted 2017-12-09T11:25:15.793

Reputation: 39 774

Answers

8

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!

ovs

Posted 2017-12-09T11:25:15.793

Reputation: 21 408

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

– Jonathan Allan – 2017-12-09T15:47:50.167

1@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... – FlipTack – 2017-12-09T16:10:39.960

6

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

J42161217

Posted 2017-12-09T11:25:15.793

Reputation: 15 931

6

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

Arnauld

Posted 2017-12-09T11:25:15.793

Reputation: 111 334

6

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

Emigna

Posted 2017-12-09T11:25:15.793

Reputation: 50 798

A possible source of inspiration from my 10-byte approach: µNγD€gs€ÙQ

– Mr. Xcoder – 2017-12-09T13:05:10.510

4

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.

Erik the Outgolfer

Posted 2017-12-09T11:25:15.793

Reputation: 38 134

2

Haskell, 70 bytes

import Data.List
filter(all(\s->read[s!!0]==length s).group.show)[1..]

Xiyng

Posted 2017-12-09T11:25:15.793

Reputation: 41

1

You can drop the leading x= as per our golfing rules.

– Laikoni – 2017-12-10T11:57:27.757

2

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

Esolanging Fruit

Posted 2017-12-09T11:25:15.793

Reputation: 13 542

2

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.

Unrelated String

Posted 2017-12-09T11:25:15.793

Reputation: 5 300

1

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)

l4m2

Posted 2017-12-09T11:25:15.793

Reputation: 5 985

"Javascript 1"? Is there such a language name? – user202729 – 2017-12-09T12:35:20.853

I mean it works since JavaScript appear, not sure if the name is correwct – l4m2 – 2017-12-09T12:37:40.130

sorry it seems not work in js1. I read and found no replace – l4m2 – 2017-12-09T12:39:55.773

1

Perl 6, 49 bytes

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

Try it online!

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

Sean

Posted 2017-12-09T11:25:15.793

Reputation: 4 136

The \d can just be . – Jo King – 2019-05-27T03:22:07.337

1

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.

MickyT

Posted 2017-12-09T11:25:15.793

Reputation: 11 735

1

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.

recursive

Posted 2017-12-09T11:25:15.793

Reputation: 8 616

0

Pyth, 12

.f.AqMr8jZ10

Test online!

user74686

Posted 2017-12-09T11:25:15.793

Reputation:

10 bytes: .fqFCr8jZT – Erik the Outgolfer – 2017-12-09T15:18:31.157

0

Perl 5 -p, 48 bytes

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

Try it online!

Returns the n-th element, 1 indexed.

Xcali

Posted 2017-12-09T11:25:15.793

Reputation: 7 671

0

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;
}

Jakob

Posted 2017-12-09T11:25:15.793

Reputation: 2 428