(([{}](((()))<>))<>){<>({}({}({})))}{}{}
小麦巫师和我就这个问题进行了决斗。当我们决定发布解决方案时,我们被捆绑在42个字节上,但是我发现他的解决方案有2个字节。我们决定将其视为平局(我的解决方案如下)。
在线尝试!
说明:
# Set up the stacks like this: -input
1 -input
1 1
(([{}](((()))<>))<>) ^
# Output 1 for triangular and 0 for non-triangular
{<>({}({}({})))}{}{}
有关完整说明,请参见Wheat Wizard的答案。
(([({})])<>){(({}())<>{}({})){((<>))}{}{}}
产出 0\n
(字面换行符)表示真实,而空字符串表示虚假。
这个想法是一直减去1再乘2再乘3直到输入。如果您按下0,则说明这是一个三角形数字,因此可以在此停下来。
在线尝试!(真实)
在线尝试!(虚假)
# 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
}
这是我发现有趣的46字节解决方案。
{<>(({}())){({}[()]<>{(<({}[()])>)}{}<>)}{}<>}
产出 0\n
(字面换行符)为真,空字符串为假。
这个想法是从输入开始按连续数字递减计数,一次为1。例如input - (1) - (1,1) - (1,1,1)
。每次减去时,如果还不为0,则会在堆栈上保留一个额外的值。这样,如果我们为0,并且在弹出时仍在减去,则会删除堆栈上的最后一个值。如果输入是三角数,我们将精确地以0结尾,并且不会弹出0。
在线尝试!诚实
在线尝试!虚假的
# 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.
}