# [GOLF] Compact WL for lengthy TeX fractal Sierpinski Triangle

GROUPS:

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:

# Update:

BTW see a similar discussion here.

2 months ago
5 Replies

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

2 months ago

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.

2 months ago

## 40 characters

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


2 months ago

# 40 characters

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

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