21
\$\begingroup\$

My userid is 100664. In binary this is 11000100100111000.

An interesting property of this number is that it can be created entirely by concatenating strings which are repeated at least twice:

11  000  100100  111  000

The first few such numbers are \$3,7,10,12,15,24,28,31,36,40,42, 43, 45,48,51,53,54,56,58,60,63,80,87,96,99,103,112,115,117,120,122,124,127\$ (let me know if I've missed any as I worked these out by hand).

Your challenge is to calculate these. (Leading zeros don't count, e.g. 9 = 001001 is not part of the sequence.)

As with all challenges, you may either:

  • Take a number \$n\$ and output the nth term
  • Take a number \$n\$ and output the first n terms
  • Take no input and output these forever

Here's a reference implementation courtesy of Bubbler

Scoring

This is , shortest wins!

Testcases

These are 0-indexed.

0 => 3
1 => 7
3 => 12
5 => 24
10 => 42
15 => 53
20 => 63
25 => 103
\$\endgroup\$
8
  • 7
    \$\begingroup\$ looks like 54((110110)), 58((11)(1010)) are missing. \$\endgroup\$
    – Razetime
    Aug 10 at 10:58
  • 11
    \$\begingroup\$ you realise that now you can never change your username again right? \$\endgroup\$
    – Jo King
    Aug 10 at 13:33
  • \$\begingroup\$ @Razetime Thanks! \$\endgroup\$
    – emanresu A
    Aug 10 at 19:27
  • 2
    \$\begingroup\$ @JoKing No, I can just rename the question \$\endgroup\$
    – emanresu A
    Aug 10 at 19:27
  • 1
    \$\begingroup\$ @LevelRiverSt 1001 cannot be formed by concatenating strings repeated twice or more (i.e. does not match the regex ^((.+)\2+)+$). 11 is not 1 1, but just 11 which contains one string that has 1 repeated twice. \$\endgroup\$
    – Bubbler
    Aug 12 at 0:49

13 Answers 13

9
\$\begingroup\$

Jelly,  12  11 bytes

BŒṖŒɠṂ’ƊƇµ#

A full program that accepts a positive integer, \$n\$, from STDIN and prints a list of the first \$n\$ emanresu numbers.

Try it online!

How?

BŒṖŒɠṂ’ƊƇµ# - Main Link: no arguments
          # - start with k=0 and count up, collecting the first n (from STDIN) k
              which are truthy under:
         µ  -   the monadic chain, f(k):
B           -     convert (k) to binary
 ŒṖ         -     all partitions (of the binary representation of k)
        Ƈ   -     filter - keep those (partitions) which are truthy under:
       Ɗ    -       last three links as a monad, f(partition):
   Œɠ       -         run-lengths of equal elements (e.g. 101,101,1,1,1,0 -> 2,3,1)
     Ṃ      -         minimum
      ’     -         decrement (vectorises) -> 0 is falsey, other numbers are truthy
                (a result of f(k) which is non-empty is truthy)
\$\endgroup\$
7
\$\begingroup\$

Python 3, 78 71 bytes

-7 thanks to m90, wasif, and ovs

import re
i=1
while[re.match('..((.+)\\2+)+$',bin(i))and print(i)]:i+=1

Try it online!

I feel like manually setting the index isn't optimal but idk how to avoid it in this situation.

Further optimizations:

Since bin(i) always prepends 0b to our string, and match always checks from the start, we can remove the slicing from [2:] and instead embed that in the regex.

By combining the match statement with an and for the print, it short circuits and only prints i if re.match returns something other than None.

And by wrapping the and statement in a list, it forces it to always be evaluated as true in our while condition.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Improvements: You can remove the slicing and put the 0b in the regexp. The ^ can be removed since re.match only starts at the beginning of the string. \$\endgroup\$
    – m90
    Aug 10 at 11:27
  • 3
    \$\begingroup\$ 72 bytes (one from m90) \$\endgroup\$
    – wasif
    Aug 10 at 11:43
  • 3
    \$\begingroup\$ 71 bytes (based on wasif's suggestion) \$\endgroup\$
    – ovs
    Aug 10 at 16:00
  • 1
    \$\begingroup\$ I've added all your suggestions, really smart optimizations there. \$\endgroup\$
    – Underslash
    Aug 10 at 20:48
  • 1
    \$\begingroup\$ @Jakque that doesn't seem to produce the same output, for example it misses 31. I'm not sure exactly what causes that, but I'd assume its overreplacing things. \$\endgroup\$
    – Underslash
    Aug 12 at 18:00
5
\$\begingroup\$

JavaScript, 63 bytes

for(n=1;;n++)/^((.+)\2+)+$/.test(n.toString(2))&&console.log(n)

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Nice! v8 has print, so you can save a lot with that. \$\endgroup\$
    – emanresu A
    Aug 10 at 10:35
4
\$\begingroup\$

Retina, 56 bytes

{T`d`10`.1*$
^0*$
1$&
/^((.+)\2+)+$/&*\(`1
01
+`10
011
1

Try it online! Link is to verbose version of code. Outputs the infinite sequence. Explanation:

{

Repeat forever.

T`d`10`.1*$

Increment the current binary value.

^0*$
1$&

If it overflows, carry the 1.

/^((.+)\2+)+$/&

Is this an Emanresu number?

*\(`

If so then print the result of the rest of the program but then restore the current binary value.

1
01
+`10
011
1

Convert from binary to decimal.

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

Jelly,  17  15 bytes

Saved 2 bytes thanks to @Dudecoinheringaahing

Takes an integer \$n\$ from STDIN and generates the first \$n\$ terms.

This is probably twice as long as it should be...

BŒṖ‘ḄŒɠ€ỊẸ€ẠṆø#

Try it online!

\$\endgroup\$
4
  • 8
    \$\begingroup\$ Oh no, Arnauld has started writing Jelly. Soon there will be no hiding from this golfing madman. (Cool stuff, nice to see you expanding your repertoire. Sorry, I don't have any useful feedback since I don't know Jelly.) \$\endgroup\$
    – Etheryte
    Aug 10 at 16:06
  • 2
    \$\begingroup\$ @Etheryte Well, I wrote my first Jelly answer almost exactly 4 years ago. But I haven't written many of them since then, that's for sure. :-p \$\endgroup\$
    – Arnauld
    Aug 10 at 16:14
  • 3
    \$\begingroup\$ I would've suggested ŒrṪ€€ \$\to\$ Œɠ€, but it looks like Jonathan might have beat me to it \$\endgroup\$ Aug 10 at 16:43
  • \$\begingroup\$ @Dudecoinheringaahing Thanks anyway! (Will update later.) \$\endgroup\$
    – Arnauld
    Aug 10 at 17:07
4
\$\begingroup\$

Brachylog, 13 bytes

ℕ≜{ḃ~cḅ∋₁ᵐ&!}

Try it online!

Generates numbers infinitely.

ℕ≜               Choose a nonnegative integer.
   ḃ             Its binary representation
    ~c           can be partitioned such that
      ḃ          if runs of equal partitions are grouped,
         ᵐ       each group
       ∋₁        has a second element.
  {       & }    Output that integer
           !     once.

I had hoped to use j here, but it can't even get close.

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

Stax, 16 12 bytes

ï├1mìsM`0°ö≈

Run and debug it

Prints the sequence forever.

-4 bytes using the partitioning idea.

\$\endgroup\$
3
\$\begingroup\$

Perl 5, 45 bytes

/^((.+)\2+)+$/&&say$.while$_=sprintf"%b",++$.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Japt, 20 bytes

Outputs the first n terms. It's really annoying me that the RegEx takes up nearly ¾ of this solution but I can't seem to come up with a shorter, non-RegEx based one :\

Ȥè"^((.+)%2+)+$"}jU

Try it

Ȥè"^((.+)%2+)+$"}jU     :Implicit input of integer U
È                        :Function taking an integer as argument
 ¤                       :  To binary string
  è                      :  Count
   "^((.+)%2+)+$"        :    RegEx /^((.+)\2+)+$/
                 }       :End function
                  jU     :Get the first U integers that return a truthy value
\$\endgroup\$
3
\$\begingroup\$

R, 85 81 bytes

while(T<-T+1)grepl("^((.+)\\2+)+$",Reduce(paste0,T%/%2^(0:log2(T))%%2))&&print(T)

Try it online!

A regex-based solution. Prints values indefinitely.

Thanks to @pajonk for saving 4 bytes!

\$\endgroup\$
7
  • \$\begingroup\$ Try it online! → "The permalink could not be decoded" \$\endgroup\$
    – Kjetil S.
    Aug 11 at 19:00
  • \$\begingroup\$ @KjetilS. Sorry truncated the link. Fixed now \$\endgroup\$ Aug 11 at 19:15
  • \$\begingroup\$ Damn, too late. I have 82: Try it online! \$\endgroup\$
    – pajonk
    Aug 11 at 19:22
  • \$\begingroup\$ @pajonk are you happy for me to use your Reduce? I can get 81 provided the print output from R is allowed. \$\endgroup\$ Aug 11 at 19:24
  • \$\begingroup\$ Combined makes 81: Try it online! \$\endgroup\$
    – pajonk
    Aug 11 at 19:25
3
\$\begingroup\$

05AB1E, 16 bytes

∞ʒb.œ2ìεγ€g2@P}à

Outputs the infinite sequence.

Try it online.

Explanation:

∞              # Push an infinite list of positive integers: [1,2,3,...]
 ʒ             # Filter this list by:
  b            #  Convert the current integer to binary
   .œ          #  Get all partitions of this binary string
     2ì        #  Prepend a 2 before each part (this is necessary because `γ` will
               #  ignore leading 0s, and thus incorrectly group "1" and "01" together)
       ε       #  Map each partition to:
        γ      #   Group adjacent equivalent elements together
         €g    #   Get the length of each group
           2@  #   Check for each length if it's >= 2
             P #   Check if all are truthy (by taking the product)
       }à      #  After the map: check if any partition is truthy (by taking the max)
               # (after which the filtered list is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

Vyxal r, 23 bytes

λbvS∑`^((.+)\\2+)+$`r;ȯ

Try it Online!

A juicy port of JavaScript. Man I do love me some regex in golfing languages. Takes n and outputs the first n terms.

\$\endgroup\$
3
  • \$\begingroup\$ Someone should invent a compressed regex golfing language \$\endgroup\$
    – pxeger
    Aug 10 at 10:52
  • \$\begingroup\$ Retinaaaaaaaaaaaaaaaaa \$\endgroup\$
    – Razetime
    Aug 10 at 10:52
  • 1
    \$\begingroup\$ @Razetime Retina sucks at sequence challenges though, since it has no repeat-until loops. \$\endgroup\$
    – Neil
    Aug 10 at 12:27
1
\$\begingroup\$

Julia 1.0, 65 bytes

!i=replace(bitstring(i+=1),r"((.+)\2+)"=>"")>""||println(i),!i;!1

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 58 is missing. 58=111010. Don't know why but some of the other answers have ^ and +$ at the start and end of the regex. \$\endgroup\$
    – Kjetil S.
    Aug 11 at 19:13
  • \$\begingroup\$ good catch! I might have to add it too \$\endgroup\$
    – MarcMush
    Aug 11 at 19:24

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.