# Length of Binary as Base 10 [OEIS A242347]

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]

• I had the output be the length of the number because this blows up very very quickly Commented Oct 28, 2022 at 13:20
• Is outputting the infinite sequence starting from 2 (i.e. $2, 4, 10, 31, 100, 330, ...$) ok? Commented Oct 28, 2022 at 16:16
• Yes, and the sequence technically has 2 as the first term Commented Oct 28, 2022 at 16:26

# Ruby, 3833 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. • 33 bytes Commented Oct 28, 2022 at 15:45 • 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~-. Commented Oct 29, 2022 at 2:03 • 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. Commented Oct 29, 2022 at 3:37

# 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

• this seems like a very odḋ answer :p (especially given that it has moLd on it - get that answer cleaned!) Commented Oct 28, 2022 at 13:26
• @lyxal - Ok, cleaned now. Commented Oct 28, 2022 at 13:42
• Thank goodness - last thing we need is spoiled code golf answers making a stench :p Commented Oct 28, 2022 at 13:44
• @lyxal that, the moLd comment, made my day today. Commented Feb 21, 2023 at 7:34

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

eLG2'_Ob


Try it online!

Port of Vyxal

# 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. # Vyxall, 87 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  # 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: # 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  • Suggested mathjax fix: $9 log_{256}(96) ≈ 7.4$ to $9 \log_{256}(96)\approx$ 7.4 Commented Oct 29, 2022 at 17:33 • why does this have the same fractional byte values as fig? Commented Oct 30, 2022 at 0:20 • @Pacmanboss256 - it uses the same character set as Fig (printable ASCII characters) Commented Oct 30, 2022 at 7:51 • @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... Commented Nov 1, 2022 at 9:11 • @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".) Commented Nov 1, 2022 at 10:30 # 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\$$. • 9 bytes if you're okay with omitting the first term Commented Oct 29, 2022 at 3:09 # 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  # 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. # 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

• You can save two bytes by just doing f x twice: Try it online! Commented Oct 29, 2022 at 1:41

# 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!

• 66 bytes Commented Oct 28, 2022 at 14:18
• the screenshot makes it look as if you've forgotten the '1' at the beginning... Commented Oct 28, 2022 at 16:12
• @DominicvanEssen - yes, that solution misses the initial '1'. It says "starting from 2" just above it. Commented Oct 28, 2022 at 16:13
• @Pacmanboss256 - A008559 starts from 2. This one (A242347) starts from 1, and goes $[1, 2, 4, 10, 31, 100, ...]$. Commented Oct 28, 2022 at 16:30
• @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... Commented Oct 28, 2022 at 16:31

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

# 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\$$.

# J, 18 bytes

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


Attempt This Online!

-2 thanks to Jonah!

• 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 Commented Nov 10, 2022 at 1:42
• Thanks! What does x mean? Is it some kind of bigint thing? Commented Nov 10, 2022 at 1:47
• 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: Commented Nov 10, 2022 at 1:53
• 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. Commented Nov 10, 2022 at 3:09

# Raku, 16 30 bytes

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


Try it online!

An expression for the lazy, infinite sequence of numbers.

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

# 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  # 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  # 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 # Charcoal, 17 bytes ≔2θＦＮ«⟦ＩＬθ⟧≔⍘Ｉθ²θ  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. ＦＮ«  Repeat n times. ⟦ＩＬθ⟧  Output its length on its own line. ≔⍘Ｉθ²θ  Cast it to decimal, then convert it to binary as a string. # Retina 0.8.2, 45 bytes .+$*:2
{:\d+
$*B +(B+)\1$1A
AB
B
}TLd
.


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 }. # 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  # 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! • 54 bytes: Try it online! Commented Oct 29, 2022 at 4:14 • Also you can remove those random trailing semicolons Commented Oct 29, 2022 at 4:15 • @Sʨɠɠan Thank you! (And yeah, the trailing semicolons were very random indeed. I guess I was thinking in C for a second.) Commented Oct 29, 2022 at 5:32 # 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)  # 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. # Perl 5 -Mbigint, 58 bytes eval'$_=2;'.'$_=new Math::BigInt($_)->to_bin;'x$_;$_=y///c


Try it online!

# ><>, 40 bytes

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


Try it online

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

# Gaia, 11 bytes

)2¤U⟪¤bd⟫⊢l


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

• Welcome to Code Golf! Commented Oct 31, 2022 at 12:33

# 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)
`