Desafio
Escreva um código que produza o código de equação matemática TeX (LaTeX) (fornecido abaixo) que digitará o Fractal do Triângulo de Sierpinski de 5 níveis. O menor código vence .
Detalhes
O TeX (e amigos como o LaTeX, etc.) é um sofisticado sistema de composição tipográfica. Ele pode renderizar expressões complexas aninhadas arbitrárias para fórmulas matemáticas. Coincidentemente, esse "complexo aninhado" também é descritivo de fractais. O seguinte é renderizado com MathJaX
by the following plain-text math-equation code consisting of nested super- and sub-scripts:
{{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}^{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}_{{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}^{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}_{{{x^x_x}^{x^x_x}_{x^x_x}}^{{x^x_x}^{x^x_x}_{x^x_x}}_{{x^x_x}^{x^x_x}_{x^x_x}}}}}
Note this is just a 5-level nesting. You do not need to generate $...$
or $$...$$
or other markup required to start/end a math equation in TeX & Co. You can preview generated TeX in many online editors, for instance: http://www.hostmath.com but you can find many others too. This question was inspired by a discussion with friends.
Update
There is a similar question but it much more general and will produce different solutions. I wanted to see really kolmogorov-complexity for a very fixed simple code that in one system (TeX) is completely explicit while in another compressed. This also address the n
instead of 5 levels comment.
fonte
Respostas:
SOGL V0.12,
1612 bytesTry it Here!
Port of Erik The Outgolfer's Python 2 answer
fonte
Python 2, 32 bytes
Try it online!
fonte
plain TeX, 29 bytes
That outputs what others have output. But if we need the code to be compilable it would be 6 bytes more
Explanation
~
is an active character in TeX, so we can give it a (new) definition.\def~#1x{{#1x_#1x^#1x}}
defines~
as a macro, so that when TeX sees~
, it does the following:x
, and call that#1
(pattern-matching).{#1x_#1x^#1x}
For example,
~ABCx
would get replaced with{ABCx_ABCx^ABCx}
.When
~~~~~x
is used,#1
is~~~~
, so the whole thing gets replaced with{~~~~x_~~~~x^~~~~x}
. And so on.Once we have the long string, we can print it out to terminal with
\message
(and ending with a\bye
so TeX stops), so\message{~~~~~x}\bye
. Or typeset the resulting expression (as a mathematical formula), by surrounding it in$
s : so$~~~~~x$\bye
.fonte
n
(rather than5
) it could be more efficient to create a macro that outputs a list ofn
tildes~
rather than writing~~~~~
. Plus it would look better if the whole expression is typeset under\scriptscriptstyle
.05AB1E, 17 bytes
Try it online!
Explanation
Other programs at the same byte-count include
fonte
"{x^x_x}"
can be reduced ._.PowerShell,
4435 bytesTry it online!
Uses string multiplication to repeatedly
-replace
x
es with the sub- and super-scripts, then output.Saved 9 bytes thanks to Joey.
fonte
"'x'"+"-replace'x','{x^x_x}'"*5|iex
is a bit easier, no?MATL,
2120 bytes-1 byte thanks to Giuseppe
Try it online!
fonte
'x'XJ5:"J'{x^x_x}'Zt
or even5pc5:"5pc'{x^x_x}'Zt
JavaScript (ES6),
454237 bytesEdit: Saved
32 bytes thanks to @Arnauld. Specifying 5 still costs me 2 bytes; this414035-byte version takes a parameter instead:fonte
05AB1E, 13 bytes
Try it online!
Port of my Python 2 answer.
fonte
Jelly, 12 bytes
Try it online!
Port of my Python 2 answer.
fonte
Japt,
212018 bytesTest it
Explanation
Generate an array of length 5 and map over it.
Split a string to an array of characters
Rejoin (
q
) to a string using the current value ofU
or (ª
)"x"
.Assign the result of that to
U
.Get the last element in the array.
Alternatives, 18 bytes
Same as above but reducing the array after it's been created.
Test it
The recursive option.
Test it
fonte
Java (OpenJDK 8),
179167 bytes@Neil port
Try it online!
fonte
t
as a real functio instead of a lambdat.apply(1)
should bet.apply(new Integer(a[0]))
instead. But why not just post a method?String t(int n){return n>0?t(n-1).replace("x","{x^x_x}"):"x";}
And if the requirement of the challenge would be a full program (which isn't), using a Java 7 recursive method would be shorter than a lambda:interface Y{static void main(String[]a){System.out.print(t(new Integer(a[0])));}static String t(int n){return n>0?t(n-1).replace("x","{x^x_x}"):"x";}}
Wolfram Language (Mathematica) - 40 characters
Summarizing 3 best answers here:
40 bytes:
41 bytes:
44 bytes:
fonte
C (gcc), 82 bytes
Try it online!
fonte
Pyth,
171613 bytesTry it online!
Python 3 translation:fonte