# An ASCII self-referential sequence

The sequence A109648 starts with the following numbers

53, 51, 44, 32, 53, 49, 44, 32, 52, 52, 44, 32,
51, 50, 44, 32, 53, 51, 44, 32, 52, 57, 44, 32,
52, 52, 44, 32, 51, 50, 44, 32, 53, 50, 44, 32,
53, 50, 44, 32, 52, 52, 44, 32, 51, 50, 44, 32,
53, 49, 44, 32, 53, 48, 44, 32, 52, 52, 44, 32,
51, 50, 44, 32, 53, 51, 44, 32, 53, ...


and has the description of

Sequence is its own ASCII representation (including commas and spaces).

This is the unique sequence with the property. Your task is compute this sequence.

Standard rules and I/O methods apply. The shortest code in bytes wins.

• so this works maybe? a[0] = 53; a[1] = 51; a[n_] := Switch[Mod[n, 4], 0, 48 + Floor[a[n/4]/10], 1, 48 + Mod[a[(n - 1)/4], 10], 2, 44, 3, 32]; Table[a[n], {n, 0, 68}] Dec 29, 2021 at 0:04
• @DialFrost is that just copied from the Mathematica entry on OEIS? Dec 29, 2021 at 0:05
• yes it is im not posting it as an answer im asking Dec 29, 2021 at 0:11
• Related Jan 5 at 21:56

# Python 2, 41 bytes

f=lambda n:ord(map(f,n*[n/4])+[5][1+n])

Attempt This Online!

Thanks to @loopywalt

Uses Python's string representation of tuples to get the ,  parts of the sequence (44, 32).

## Python 2, 42 bytes

f=lambda n:ord("5"[n:]orf(n/4),1[1+n%4])

Attempt This Online!

Here are some variations, but I've not been able to get any of them shorter:

f=lambda n:ord(5*(n<1)or f(n/4),1[1+n%4])
f=lambda n:ord(0**n*5or f(n/4),1[1+n%4])
f=lambda n:ord([5][n:]or[f(n/4)]*n[1+n])
f=lambda n:ord([5]+[n and f(n/4)]*n[1+n])
f=lambda n:ord(0**n*-5or[f(n/4)]*n[1+n])
f=lambda n:ord(-~-n*"5"or[f(n/4)]*n[1+n])

• 41! Very slow, though. Times out around term 200 or so. ato.pxeger.com/… Dec 30, 2021 at 1:31
• @loopywalt 41 factorial is way too many bytes! Dec 30, 2021 at 3:55

# JavaScript (Node.js), 39 bytes

f=n=>n?[...f(n>>2)+'',28,16][n%4]^48:53


Try it online!

• What does the ^48 do? Dec 29, 2021 at 2:19
• @Jonah ^ is xor operator. xor 48 so convert number to its ASCII.
– tsh
Dec 29, 2021 at 2:31
• More specifically, ^48 is the same as +48 which gets a number's ASCII code (since 0 is 48 and the digits increase from there), but 28^48 also gives 44 (ASCII for ,) and 16^48 gives 32 (ASCII for space) Dec 29, 2021 at 8:35

# Wolfram Language (Mathematica), 585755 48 bytes

-1 (-2) thanks to alephalpha

Nest[Rest@*ToCharacterCode@*ToString,!5,#][[#]]&


Try it online!

Returns the $$\n\$$th element, 1-indexed.

### Wolfram Language (Mathematica), 68 bytes

If[#<1,53,Join[48+IntegerDigits@#0@⌊#/4⌋,{44,32}][[#~Mod~4+1]]]&


Try it online!

Implements the formula given on the OEIS page.

• I guess we approached this in the same way... Can you make a version using the formula? Dec 29, 2021 at 0:47
• what does #0 do? Dec 29, 2021 at 0:48
• @Jonah It refers to the enclosing function. See this tips post.
– att
Dec 29, 2021 at 1:10
• If[#<1,{5},ToCharacterCode[ToString@#0[#-1]][[2;;#+1]]]& Dec 30, 2021 at 12:07

# Charcoal, 26 bytes

≔⊗⊖⊗Ｎθ≔5ηＷ‹Ｌηθ≔⪫Ｅη℅κ, η…ηθ


Try it online! Link is to verbose version of code. Outputs the first n elements, joined with comma and space. Explanation:

≔⊗⊖⊗Ｎθ


Calculate the length of the desired output.

≔5η


Start with the initial 5, whose ASCII code starts with itself.

Ｗ‹Ｌηθ


Until the string is long enough...

≔⪫Ｅη℅κ, η


... join its ASCII codes with comma space.

…ηθ


Output the desired number of terms.

# Vyxal, 10 bytes

5?(‛, jC)Ẏ


Try it Online!

Outputs the first n terms, although it actually calculates the first n^3 terms.

5          # Starting with 5
?(     )  # input times...
‛, j    # Join by ,
C   # Get charcodes
Ẏ # At end, get first n

• Your second answer will never terminate, and therefore never print anything Dec 29, 2021 at 0:11
• @pxeger Oh ok :( Dec 29, 2021 at 0:12

# Haskell + hgl, 24 bytes

q=53:tl(Or<ic", "(sh<q))


This defines q an infinite list representing the sequence.

If you want the actual string of the sequence you can do that for 1 more byte:

q=ic", "$sh<Or<('5':tl q)  ## Explanation So here we define q in terms of itself. A naive answer might look like: q=Or<ic", "(sh<q)  Which takes q maps show across it to convert all the numbers to strings, intercalates ", " with ic, to get the string and maps Or to get the char points. However if we define this the resulting list is just an infinite loop and never manages to produce the first element. So we need to tell it what the first element is. To do this we take what we had, chop off the first element and put the correct answer in it's place. This allows Haskell to skip to the second element when calculating this list which it can calculate in terms of the first element just fine. And from there every successive element can be calculated just fine. # J, 40 37 bytes $_2".@}.[:,0&(]', ',~"#.3":"+@u:":)&5


Try it online!

-3 thanks to Bubbler's idea of taking first n terms with $ instead of returning the nth term Inspired by emanrasu A's answer Yikes. • A quick golf: ]0&(...)5: -> 0&(...)&5 Dec 29, 2021 at 2:33 • And change <:{ to $ to get first n terms instead of the n-th term. Dec 29, 2021 at 2:39

# Stax, 10 bytes

▓░δΓ╙öâ'╜Ä


Run and debug it

freezes at 10.

# C (gcc), 76 bytes

f(n){char*s=calloc(n,8),*t=s;for(*s=53;n--;)t+=sprintf(t,"%d, ",*s++);n=*s;}


Try it online!

Inputs a $$\0\$$-based index $$\n\$$.
Returns the $$\n^\text{th}\$$ element.

# R, 837974 59 bytes

function(n){x=5
for(i in 1:n)x=utf8ToInt(toString(x))
x[n]}


Try it online!

Credit to Dominic van Essen for -4 bytes

• Nice! 79 bytes but horribly inefficient... Dec 29, 2021 at 15:04

# BQN, 6335 28 bytes

Edit: -28 and then -7 more bytes (!) thanks to Razetime

{𝕩↑{-⟜@¨∾∾⟜", "¨•Fmt¨𝕩}⍟𝕩⥊5}


Try it at BQN online REPL

Outputs first n elements of sequence for input n. But, to get this, calculates the first 4^n elements. So times-out (or crashes) on the online REPL for even moderate n.

Ungolfed, commented (try it here):

Log10 ← 10⊸⋆⁼                                        # base 10 logarithm
N2d ← Number_to_digits ← {(10⋆⌽↕⌈Log10 𝕩)(10|·⌊÷)˜𝕩} # get base 10 digits
N2s ← Number_to_string ← '0'⊸+¨ N2d                  # string representation
S2a ← String_to_ASCII ← @⊸(-˜)¨                      # ASCII codes of string

ASCII_srs ← {𝕩↑{S2a∾´(∾⟜", ")∘N2s¨𝕩}⍟𝕩 ⟨5⟩}
⍟𝕩       # repeat input times:
N2s¨𝕩}         #  get string representations,
(∾⟜", ")∘               #  join ", " to each,
∾´                         #  join it all together,
{S2a                           #  and convert to ASCII codes;
{𝕩↑                               # finally get input elements.

• @⊸(-˜)¨ -> -⊸@¨, ∾ does the same thing as ∾´. A golfier but worse way to get N2d would be '0'-˜•Fmt Dec 31, 2021 at 7:29
• Nice! Thanks v much (x3)! Dec 31, 2021 at 16:07
• You don't need the ˜ in @˜. You can change (∾⟜", ")∘{•Fmt𝕩}¨ to ∾⟜", "¨•Fmt¨, and ⟨5⟩ is ⥊5 or ⋈5. Jan 1 at 3:35
• @Razetime - Thanks again (very belatedly). I didn't update this at the time because I didn't understand it well enough then, and subsequently forgot about it... May 2 at 13:17

# Ruby, 40 bytes

->n{a=*5
n.times{a=(a*", ").bytes}
a[n]}


Try it online!

a lambda that returns the n-th element

# Wolfram Language (Mathematica), 83 bytes

a@1={53};a[n_]:=a[n]=Join[a[n-1],ToCharacterCode@StringTake[ToString@a[n-1],{n+1}]]


Try it online!

# Perl 5 + -pa, 42 bytes

$_=53;$_.=", ".ord+(/./g)[++$;]while"@F">$


Try it online!

## Explanation

Naiive approach, builds the string from 53 and appends the ordinal values. 0-indexed, prints up to the nth term.

Uses \$; to store the counter to save a byte at the end from the -p flag.

# Pari/GP, 49 bytes

f(n)=if(n,Vec(Vecsmall(Str(f(n-1)))[2..n+1]),[5])


Try it online!

1-indexed. Based on the PARI/GP code on the OEIS page.

# Japt, 15 bytes

@Zq,ú2)cY}h#5ìL


Try it

# Excel VBA, 69 50 bytes

Saved 19 bytes thanks to a total re-write by Taylor Raine

?"53";:t=3:Do:k=", "&Asc(t):t=Mid(t &k,2):?k;:Loop


The function is typed directly into the immediate window and output is in the same window. It will run until it's stopped (ESC) or it hits a software / hardware limitation like running out memory.

Colons are line line breaks in VBA. Here's a version with some nicer formatting and comments.

?"53";                 ' Print "53" without a line break at the end
t = 3                  ' Seed the value for t
Do                     ' Start a loop
k = ", " & Asc(t)   ' Store the ASCII value for the first character in t
t = Mid(t & k, 2)   ' Set t be everything in t & k except the first character
?k;                 ' Print the value of k without a line break at the end
Loop                   ' Go back to "Do"

• You can get this down to 50 bytes by rearranging and converting to an immediate window function that outputs to VBE immediate window - ?"53";:t=3:Do:k=", "&Asc(t):t=Mid(t &k,2):?k;:Loop Jan 7 at 16:31

# APL+WIN, 60 bytes

Prompts for required term number. Index origin = 0

i←0⋄s←53 51 44 32⋄⍎∊(n←⎕)⍴⊂'s←s,(⎕av⍳⍕s[i←i+1]),44 32⋄'⋄s[n]


TIO examples generate the first column of the tabular form of the series as depicted in the question.

Try it online! Thanks to Dyalog Classic

# Husk, 17 bytes

↑¹!¡ȯmcJ", "ms;53


Try it online!

# Husk, 16 bytes

↑¹!¡ȯJ", "mosc"5


With output as character string (ASCII values represent sequence A109648)

Try it online!

# 05AB1E, 9 bytes

5Δ„, ýÇI£


Outputs the first $$\n\$$ results.

Try it online.

Explanation:

5          # Start with a 5
Δ         # Loop until the result no longer changes:
„, ý     #  Join with ", " delimiter
Ç    #  Convert it to a list of its codepoint integers
I£  #  Only leave the first input amount of values
# (after the loop, the resulting list is output implicitly)