19
\$\begingroup\$

Computers like binary. Humans like base 10. Assuming users are humans, why not find the best of both worlds? Your task is to find the first n terms in the sequence A008559 where each term is the binary representation of the previous number interpreted as a base 10 number.

Input

An integer greater than 0. Note that the first value of A008559 is 2.

Output

Just to make it a little more readable, the output should be the number of digits in the Nth term of A008559, which is A242347. Invalid inputs can output whatever, standard rules apply.

Scoring

This is so shortest bytecount wins, no standard loopholes etc...

Test Cases

2 -> [1,2]
5 -> [1,2,4,10,31]
10 -> [1,2,4,10,31,100,330,1093,3628,12049]
20 -> [1,2,4,10,31,100,330,1093,3628,12049,40023,132951,441651,1467130,4873698,16190071,53782249,178660761,593498199,1971558339]
\$\endgroup\$
3
  • \$\begingroup\$ I had the output be the length of the number because this blows up very very quickly \$\endgroup\$
    – pacman256
    Commented Oct 28, 2022 at 13:20
  • \$\begingroup\$ Is outputting the infinite sequence starting from 2 (i.e. \$2, 4, 10, 31, 100, 330, ...\$) ok? \$\endgroup\$
    – The Thonnu
    Commented Oct 28, 2022 at 16:16
  • \$\begingroup\$ Yes, and the sequence technically has 2 as the first term \$\endgroup\$
    – pacman256
    Commented Oct 28, 2022 at 16:26

29 Answers 29

7
\$\begingroup\$

Ruby, 38 33 25 bytes

a=2;loop{p /$/=~a="%b"%a}

Attempt This Online! Outputs the sequence indefinitely starting from 2.

Thanks for a whopping -13 bytes from a collective effort by south and Dingus.

\$\endgroup\$
3
  • \$\begingroup\$ 33 bytes \$\endgroup\$
    – south
    Commented Oct 28, 2022 at 15:45
  • \$\begingroup\$ It looks like you don't need .to_i, and if you start the sequence from 2 (which OP allowed in the comments) you can remove the initial p~-. \$\endgroup\$
    – Dingus
    Commented Oct 29, 2022 at 2:03
  • \$\begingroup\$ Good catch, that brings it down to 25 a=2;loop{p /$/=~a="%b"%a}. I thought to try it without #to_i, but I guess I didn't idk. \$\endgroup\$
    – south
    Commented Oct 29, 2022 at 3:37
6
\$\begingroup\$

Husk, 9 9 bytes

¡o§,ḋLd;2

Try it online!

Outputs the infinite sequence.


The previous version (also 9 bytes when outputting the infinite sequence) was moLd¡odḋ2, but I've cleaned this up at the request of lyxal, who considered it too odd due to the moLd in it.

         2 # starting with 2
     ¡     # construct an infinite sequence
      o    # by repeatedly applying 2 functions:
        ḋ  #   convert to binary digits
       d   #   convert from base-10 digits
 m         # now map over this infinite list
  o        # using 2 functions:
   L       #   length of
    d      #   base-10 digits
\$\endgroup\$
4
  • 1
    \$\begingroup\$ this seems like a very odḋ answer :p (especially given that it has moLd on it - get that answer cleaned!) \$\endgroup\$
    – lyxal
    Commented Oct 28, 2022 at 13:26
  • 3
    \$\begingroup\$ @lyxal - Ok, cleaned now. \$\endgroup\$ Commented Oct 28, 2022 at 13:42
  • \$\begingroup\$ Thank goodness - last thing we need is spoiled code golf answers making a stench :p \$\endgroup\$
    – lyxal
    Commented Oct 28, 2022 at 13:44
  • \$\begingroup\$ @lyxal that, the moLd comment, made my day today. \$\endgroup\$ Commented Feb 21, 2023 at 7:34
5
\$\begingroup\$

Fig, \$8\log_{256}(96)\approx\$ 6.585 bytes

eLG2'_Ob

Try it online!

Port of Vyxal

\$\endgroup\$
5
\$\begingroup\$

K (ngn/k), 14 bytes

{#$x(10/2\)/2}

Try it online!

Outputs the nth element.

Unfortunately, this can only do the first 3 elements because it's written in C, so overflow happens very quickly.

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

Vyxal l, 8 7 5 bytes

2?(ΠE

Try it Online!

Outputs nth term

Explained

2?(ΠE
2      # Push 2 to the stack
 ?(    # Input times:
   ΠE  #   push int(binary representation of top of stack)
       # the `l` flag pushes the length of the top of the stack

Old 7 byter, outputs infinite sequence

2‡ΠEḞvL

Try it Online!

Explained

2‡ΠEḞẎvL
2‡  Ḟ    # an infinite generator that gets its next term by:
  ΠE     #   converting its previous term to binary and casting to int   
      vL # lengths of every item
\$\endgroup\$
2
4
\$\begingroup\$

Factor, 35 bytes

2 [ >bin dup length . dec> t ] loop

Prints the infinite sequence starting with 2. Times out on TIO, but here's a screenshot using the debugger to step through some of the starting iterations:

enter image description here

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

Sequences, \$9 \log_{256}(96)\approx\$ 7.4 bytes

2HE2hBnHz

Explanation

2HE2hBnHz
  E        // Infinite sequence
2H         // Starting with 2
           // For each term:
   2hB     //   Convert to binary
      n    //   Interpret as integer
       H   //   And use this as the next term
        z  //   Get the length and output implicitly
\$\endgroup\$
7
  • \$\begingroup\$ Suggested mathjax fix: \$9 log_{256}(96) ≈ 7.4\$ to \$9 \log_{256}(96)\approx\$ 7.4 \$\endgroup\$
    – Seggan
    Commented Oct 29, 2022 at 17:33
  • \$\begingroup\$ why does this have the same fractional byte values as fig? \$\endgroup\$
    – pacman256
    Commented Oct 30, 2022 at 0:20
  • 1
    \$\begingroup\$ @Pacmanboss256 - it uses the same character set as Fig (printable ASCII characters) \$\endgroup\$
    – The Thonnu
    Commented Oct 30, 2022 at 7:51
  • \$\begingroup\$ @TheThonnu - it looks from the 'sequences' 'list of commands' page that there are only 57 commands. After adding the decimal digits, you could presumably decide to score yourself as 9 log256(67) ≈ 6.82435 bytes... although I notice that most conventional programming languages that also only use printable ASCII values + newlines don't seem very keen to adopt this scoring strategy... \$\endgroup\$ Commented Nov 1, 2022 at 9:11
  • \$\begingroup\$ @DominicvanEssen - the other characters, even if they are not commands, could be used in strings, so I don't want to rule out that possibility. (For example, not including w would make sequences incapable of using the string "Hello world".) \$\endgroup\$
    – The Thonnu
    Commented Nov 1, 2022 at 10:30
3
\$\begingroup\$

Pyth, 10 bytes

L.BsbmlyF2

Test suite

Prints the first \$n\$ terms.

The m can be omitted to instead print \$a(n)\$, where \$a(0)=2\$.

\$\endgroup\$
1
  • \$\begingroup\$ 9 bytes if you're okay with omitting the first term \$\endgroup\$ Commented Oct 29, 2022 at 3:09
3
\$\begingroup\$

Pip, 10 bytes

Y2LaP#TB:y

Try It Online!

Prints the first a terms in the sequence starting with 2, where a is the argument input.

Y2LaP#TB:y      ; a = input
Y2              ; Set y = 2
  La            ; Loop the following code "a" times...
    P#          ; Print the length of...
      TB:y      ; y treated as a decimal number converted to a binary string
                  and set y to that value
\$\endgroup\$
3
\$\begingroup\$

APL (dzaima/APL), 19 bytes

{≢⍕(10⊥2∘⊥⍣¯1)⍣⍵⊢2}

Try it online!

Due to limits, this can also only output the first 5 elements.

This works in Dyalog too, but can only output the first 3 elements there.

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

Haskell, 75 63 bytes

g 2
g x=length(show$f x):g(f x)
f 0=0
f n=f(div n 2)*10+mod n 2

Try it online!

  • Thanks to @Sʨɠɠan for saving 2 Bytes

f n returns a (decimal binary, length) tuple

\$\endgroup\$
1
  • \$\begingroup\$ You can save two bytes by just doing f x twice: Try it online! \$\endgroup\$
    – naffetS
    Commented Oct 29, 2022 at 1:41
3
\$\begingroup\$

Python, 56 47 40 bytes

  • -9 thanks to friddo.
  • -7 thanks to loopy walt.

Outputs the infinite sequence, starting from 2. (Note: apparently this is fine)

i=2
while i:=f"{int(i):b}":print(len(i))

Attempt This Online!


Python, 66 59 bytes

  • -7 thanks to loopy walt.

Suggested by friddo. Returns the first n terms.

lambda n,i=2:[1]+[len(i:=f"{int(i):b}")for _ in range(1,n)]

Attempt This Online!


Python, 74 69 bytes

  • -5 thanks to loopy walt.

Returns the nth term.

lambda n,i=2:len((['1']+[(i:=f"{int(i):b}")for _ in range(n-1)])[-1])

Attempt This Online!

\$\endgroup\$
14
  • 1
    \$\begingroup\$ 66 bytes \$\endgroup\$
    – friddo
    Commented Oct 28, 2022 at 14:18
  • \$\begingroup\$ the screenshot makes it look as if you've forgotten the '1' at the beginning... \$\endgroup\$ Commented Oct 28, 2022 at 16:12
  • \$\begingroup\$ @DominicvanEssen - yes, that solution misses the initial '1'. It says "starting from 2" just above it. \$\endgroup\$
    – The Thonnu
    Commented Oct 28, 2022 at 16:13
  • 1
    \$\begingroup\$ @Pacmanboss256 - A008559 starts from 2. This one (A242347) starts from 1, and goes \$[1, 2, 4, 10, 31, 100, ...]\$. \$\endgroup\$
    – The Thonnu
    Commented Oct 28, 2022 at 16:30
  • 1
    \$\begingroup\$ @pacmanboss - you're confusing the two sequences: A008559 must start from 2, but, as a result, A242347 starts from 1, although I see that you've allowed it to start at the second term for this challenge. Edit: as The Thonnu also wrote while I was typing... \$\endgroup\$ Commented Oct 28, 2022 at 16:31
3
\$\begingroup\$

PARI/GP 55 bytes

f(n)=k=2;d=digits;while(n--,k=fromdigits(d(k,2)));#d(k)

Stack of 8 GBytes overflows for n=18.

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

C (gcc), 67 bytes

q;b;v;f(n){for(v=2;n--;v=q)for(b=q=0;v;v/=2)q+=v%2*exp10(b++);q=b;}

Try it online!

Inputs \$1\$-based \$n\$.
Returns the \$n^{\text{th}}\$ element starting with \$a(1) = 2\$.

\$\endgroup\$
3
+100
\$\begingroup\$

J, 18 bytes

[:#@":10x&(#.#:)&2

Attempt This Online!

-2 thanks to Jonah!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Nice, I will give your bounty back! Couple notes: you need 10x or you'll get wrong answers for 5 and above. Note sure if 0 indexing like this uses is allowed here, but assuming it is here's a tacit version for 18: [:#@":10x&(#.#:)&2 \$\endgroup\$
    – Jonah
    Commented Nov 10, 2022 at 1:42
  • \$\begingroup\$ Thanks! What does x mean? Is it some kind of bigint thing? \$\endgroup\$
    – naffetS
    Commented Nov 10, 2022 at 1:47
  • \$\begingroup\$ Exactly. It stands for "eXtended precision". 10x is a way to write it as a literal. You could also do x:10 to convert using the verb x: \$\endgroup\$
    – Jonah
    Commented Nov 10, 2022 at 1:53
  • \$\begingroup\$ Ah, nice. I searched for J bigints but didn't find anything. It's a pain to use google with J because it doesn't go well with only one letter. (Why does it have to be called J?) I usually just search jsoftware, but that narrows it down a little. Also, the names of verbs in the vocabulary are very vague often. \$\endgroup\$
    – naffetS
    Commented Nov 10, 2022 at 3:09
3
\$\begingroup\$

Raku, 16 30 bytes

map *.chars,(2,+*.base(2)...*)

Try it online!

An expression for the lazy, infinite sequence of numbers.

\$\endgroup\$
2
  • \$\begingroup\$ The challenge asks you to output the length, not the number itself \$\endgroup\$
    – naffetS
    Commented Nov 9, 2022 at 23:35
  • \$\begingroup\$ @Sʨɠɠan Huh...dunno how I missed that. Fixed. \$\endgroup\$
    – Sean
    Commented Nov 10, 2022 at 7:39
2
\$\begingroup\$

Jelly,  8  7 bytes

‘ḊḌB$\Ẉ

A monadic Link that accepts a positive integer, \$n\$, and yields a list of the first \$n\$ terms.

Try it online!

How?

Performs each conversion step in the opposite order as it avoids the need to get lists again at the end.

Uses a reduce that only uses the left item at each step, starting with a list of length \$n\$ that starts with a \$2\$ as it saves a byte over collecting up \$n-1\$ times starting with a \$2\$.

‘ḊḌB$\Ẉ - Link: integer, n
‘       - increment (n) -> n+1
 Ḋ      - dequeue ([1..n+1]) -> [2..n+1]
     \  - reduce by:
    $   -   last two links as a monad - i.e. f(left):
 Ḍ      -     convert from decimal e.g. 2 -> 2     or [1,0,1,0] -> 1010
  B     -     convert to binary    e.g. 2 -> [1,0] or 1010 -> [1,1,1,1,1,1,0,0,1,0]
      Ẉ - length of each
\$\endgroup\$
2
\$\begingroup\$

Nibbles, 7.5 bytes (15 nibbles)

.`.2`@~``@$,`p
   2            # start with 2
 `.             # iterate while unique:
       ``@$     #   convert to bits
    `@~         #   and convert to base 10
.               # now, map over this infinite list
           ,    #   get lengths of
            `p  #   string representations

enter image description here

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

sclin, 21 bytes

"2""2X>b >S"itr"len"map

Try it here! Returns an infinite list.

For testing purposes (use -i flag if running locally):

; 10tk >A
"2""2X>b >S"itr"len"map

Explanation

Prettified code:

"2" ( 2X>b >S ) itr \len map
  • "2" ( ... ) itr generate infinite list starting from 2...
    • 2X>b to binary
    • >S to string
  • \len map get lengths of each element
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 17 bytes

≔2θFN«⟦ILθ⟧≔⍘Iθ²θ

Try it online! Link is to verbose version of code. As we are outputting A242347, which starts with 1, so does this version. Explanation:

≔2θ

Start with 2.

FN«

Repeat n times.

⟦ILθ⟧

Output its length on its own line.

≔⍘Iθ²θ

Cast it to decimal, then convert it to binary as a string.

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

Retina 0.8.2, 45 bytes

.+
$*:2
{`:\d+
$*B
+`(B+)\1
$1A
AB
B
}T`L`d
.

Try it online! Outputs the 0-indexed nth term. Link includes test cases for 0 to 3 as higher values are too slow. Explanation:

.+
$*:2

Convert the input to unary as a number of :s and suffix a 2 for the zeroth value.

{`
}`

Repeat until the nth value has been found.

:\d+
$*B

If more terms are needed then convert the current value into unary as a number of Bs.

+`(B+)\1
$1A
AB
B

If there are any Bs then convert them to binary using B for 1 and A for 0. This is done separately to avoid corrupting the final value.

T`@=`d

Convert the "binary" to normal digits.

.

Count the number of digits in the final value.

A008559 can be obtained at a saving of 3 bytes by deleting the last line and removing the }.

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

cQuents, 9 bytes

Lb$
=2:JZ

Try it online!

Prints the entirely sequence infinitely starting with 1,2,4,10,31,100,330,...

Explanation

First line

       implicit : output the nth term if input is provided, or all terms in the sequence if no input provided
       each term in the sequence equals
L                                       length (                       )
 b                                               second line (       )
  $                                                            index

Second line

=2     first term in sequence is 2
  :    output the nth term in the sequence
       each term in the sequence equals
   J                                    base 2 of (                   )
    Z                                               the previous term
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 54 bytes

-3 bytes thanks to @Sʨɠɠan

Prints the sequence indefinitely, starting at \$2\$ (as now allowed by the OP).

for(n=2;;console.log(n.length))n=BigInt(n).toString(2)

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 54 bytes: Try it online! \$\endgroup\$
    – naffetS
    Commented Oct 29, 2022 at 4:14
  • 1
    \$\begingroup\$ Also you can remove those random trailing semicolons \$\endgroup\$
    – naffetS
    Commented Oct 29, 2022 at 4:15
  • \$\begingroup\$ @Sʨɠɠan Thank you! (And yeah, the trailing semicolons were very random indeed. I guess I was thinking in C for a second.) \$\endgroup\$
    – Arnauld
    Commented Oct 29, 2022 at 5:32
2
\$\begingroup\$

05AB1E, 6 bytes

Infinite sequence starting at 2:

Tλb}€g

Try it online.

Could be the infinite sequence starting at 1 by replacing T with 2: 2λb}€g - try it online.

Outputting the \$n^{th}\$ term is 6 bytes as well with either 2λèb}g (try it online) or 2IFb}g (try it online);
and outputting the first \$n\$ values is 7 bytes with either 2λ£b}€g (try it online) or 2IFbDg, (try it online).

Explanation:

 λ      # Start a recursive environment,
        # to output the infinite sequence
T       # Starting at a(0)=10
        # Where every following a(n) is calculated as:
        #  (implicitly push the previous term a(n-1))
  b     #  Convert it from a base-10 integer to a binary string
   }€   # After we have the infinite sequence: map over each binary string:
     g  #  Pop and push its length
        # (after which the infinite sequence is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 7 bytes

2æhpià∟

Prints the infinite sequence, starting from 1.

Try it online.

Outputting the first \$n\$ terms would be 8 bytes instead:

2kæhpià;

Try it online.

Explanation:

2        # Push a 2
      ∟  # Do-while true (without popping),
 æ       # using 4 characters as inner code-block:
  h      #  Push the length (without popping)
   p     #  Pop and print it with trailing newline
    i    #  Convert the binary-string to a base-10 integer
     à   #  Convert that integer to a binary-string

The program for the first \$n\$ terms is pretty similar, except that k pushes the input-integer; æ acts as a loop that many times instead of the do-while loop; and ; discards the final binary-string after the loop, which would otherwise have been output implicitly.

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

Perl 5 -Mbigint, 58 bytes

eval'$_=2;'.'$_=new Math::BigInt($_)->to_bin;'x$_;$_=y///c

Try it online!

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

><>, 40 bytes

2:2(?v:2%$2,:1%-!
?v00.>lnao>l1)
.>a*+a1

Try it online

"Infinte sequence". Overflows at the 7th number.

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

Gaia, 11 bytes

)2¤U⟪¤bd⟫⊢l

Try it online! or Try a test suite!

A sort of copy of the Jelly answer but in Gaia. Times out for >= 12 I think.

Explained

)2¤U⟪¤bd⟫⊢l
)            # Increment the input
 2¤          # Push 2 "under" the incremented input by pushing 2 and then swapping
   U         # Push a range [2, input + 1] to the stack
    ⟪   ⟫⊢   # Reduce that list by:
     ¤b      #   Converting the last item to binary
       d     #   And then to decimal
         l   # Get the length of the result
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$
    – pacman256
    Commented Oct 31, 2022 at 12:33
1
\$\begingroup\$

Pyt, 8 bytes

2`ĐąŁƥɓł

Try it online!

Prints indefinitely.

2           push 2
 `     ł    do... while top of stack is truthy
  Đ         Đuplicate
   ą        convert to ąrray of digits
    Łƥ      ƥrint Łength
      ɓ     convert to ɓinary string (implicitly treats output as decimal number)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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