Vitaliyk
(Vitaliy Kaurov)
September 18, 2017, 9:58pm
1
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 .
7 Likes
Vitaliyk
(Vitaliy Kaurov)
September 18, 2017, 10:05pm
2
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]
5 Likes
szhorvat
(Szabolcs Horvát)
September 18, 2017, 10:16pm
3
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.
5 Likes
sghuisman
(Sander Huisman)
September 19, 2017, 9:12pm
4
40 characters
Nest["{"<>#<>"_"<>#<>"^"<>#<>"}"&,"x",5]
Didn’t know about SubstitutionSystem!
2 Likes
Vitaliyk
(Vitaliy Kaurov)
September 20, 2017, 11:46am
5
40 characters
Reformulating {}-bracketing in last Szabolcs solution cuts 5 characters from it:
Nest[StringReplace["x"->"{x^x_x}"],"x",5]
Vitaliyk
(Vitaliy Kaurov)
September 20, 2017, 3:39pm
6
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
1 Like