(([{}](((()))<>))<>){<>({}({}({})))}{}{}
Wheat Wizard y yo tuvimos un duelo sobre esta pregunta. Cuando decidimos publicar nuestras soluciones, estábamos atados a 42 bytes, pero encontré un golf de 2 bytes de su solución. Decidimos que contaría como el desempate (mi solución está a continuación).
Pruébalo en línea!
Explicación:
# Set up the stacks like this: -input
1 -input
1 1
(([{}](((()))<>))<>) ^
# Output 1 for triangular and 0 for non-triangular
{<>({}({}({})))}{}{}
Para obtener una explicación completa, consulte la respuesta de Wheat Wizard .
(([({})])<>){(({}())<>{}({})){((<>))}{}{}}
Salidas 0\n(nueva línea literal) para verdadero, y la cadena vacía para falso.
La idea es restar 1, luego 2 y luego 3 hasta la entrada. Si presiona 0, entonces sabe que este es un número triangular, por lo que puede detenerse allí.
Pruébalo en línea! (verdad)
Pruébalo en línea! (falso)
# Push -input on both stacks. One is a counter and the other is a running total
(([({})])<>)
# Count up from -input to 0
{
# Push the new total which is: (counter += 1) + total (popped) + input (not popped)
# This effectively adds 1, then 2, then 3 and so on to the running total
(({}())<>{}({}))
# If not 0
{
# Push to 0s and switch stacks to "protect" the other values
((<>))
# End if
}
# Pop the two 0s, or empty the stack if we hit 0
{}{}
# End loop
}
Aquí hay una solución de 46 bytes que me pareció interesante.
{<>(({}())){({}[()]<>{(<({}[()])>)}{}<>)}{}<>}
Salidas 0\n(nueva línea literal) para verdadero, la cadena vacía para falso.
La idea es contar desde la entrada por números consecutivos, 1 a la vez. Por ej input - (1) - (1,1) - (1,1,1). Cada vez que restamos, si aún no estamos en 0, dejamos un valor adicional en la pila. De esa manera, si estamos en 0 y seguimos restando cuando explotamos, eliminamos el último valor en la pila. Si la entrada fue un número triangular, terminaremos exactamente en 0 y no resaltaremos el 0.
Pruébalo en línea! Truthy
Pruébalo en línea! falso
# Implicit input (call it I)
# Until we reach 0, or the stack is empty
{
# Add 1 to the other stack and push it twice. This is our counter.
<>(({}()))
# While counter != 0
{
# counter -= 1
({}[()]
# if I != 0
<>{
# I -= 1, and push 0 to escape the if
(<({}[()])>)
# End if
}
# Pop from the stack with I. This is either the 0 from the if, or I
{}
# Get ready for next loop End while
<>)
# End While
}
# Pop the counter that we were subtracting from
{}<>
# End Until we reach 0, or the stack is empty.
}