Message Boards Message Boards

[GOLF] Compact WL for lengthy TeX fractal Sierpinski Triangle

Code Golf is a fun coding competition with participants aiming for the shortest possible code that implements an algorithm. But please feel free to post ANY code that solves the problem - just to have fun!


GOAL: write compact Wolfram Language (WL) code that generates TeX code given below to render fractal known as Sierpinski Triangle.


I've recently seen a tremendously lengthy TeX code that produced a nicely rendered Sierpinski triangle posted by @Szabolcs Horvát on social media. Because Wolfram Community has built-in MathML plugin (based on LaTeX ) we can see result right here!

$${{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}^{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}_{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}}^{{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}^{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}_{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}}_{{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}^{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}_{{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}^{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}_{{{x}^{x}_{x}}^{{x}^{x}_{x}}_{{x}^{x}_{x}}}}}$$

with the TeX code being:

enter image description here

Update:

BTW see a similar discussion here.

POSTED BY: Vitaliy Kaurov
5 Replies

I posed this question on Code Golf at stackexchange. Feel free to respond there if you are into it, no Wolfram Language answers yet ;-) Seems to be popular. Answer in Jelly is just 12 bytes ;-)

POSTED BY: Vitaliy Kaurov

40 characters

Reformulating {}-bracketing in last Szabolcs solution cuts 5 characters from it:

Nest[StringReplace["x"->"{x^x_x}"],"x",5]
POSTED BY: Vitaliy Kaurov

40 characters

Nest["{"<>#<>"_"<>#<>"^"<>#<>"}"&,"x",5]

Didn't know about SubstitutionSystem!

POSTED BY: Sander Huisman

Well, it really wasn't meant for golf originally ... if we're going to do that, I propose ...

51 characters

Last@SubstitutionSystem[{"x"->"{x}_{x}^{x}"},"x",5]

48 characters

Nest[StringReplace[#,"x"->"{x}^{x}_{x}"]&,"x",5]

45 characters

Nest[StringReplace["x"->"{x}^{x}_{x}"],"x",5]

P.S. It's a pity that TeXForm does not support Subsuperscript.

POSTED BY: Szabolcs Horvát

58 characters

I change a bit TeX code, but it still produces the properly rendered fractal Sierpinski Triangle. I built a bit bigger one for fun.

s = ToString;Nest["{"<>s[#]<>"_"<>s[#]<>"^"<>s[#]<>"}"&,x,6]
POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract