(([{}](((()))<>))<>){<>({}({}({})))}{}{}
Wheat Wizard and I had a duel over this question. When we decided to post our solutions we were tied at 42 bytes, but I found a 2 byte golf of his solution. We decided that would count as the tie breaker (my solution is below).
Try it online!
Explanation:
# Set up the stacks like this: -input
1 -input
1 1
(([{}](((()))<>))<>) ^
# Output 1 for triangular and 0 for non-triangular
{<>({}({}({})))}{}{}
For a full explanation please see Wheat Wizard's answer.
(([({})])<>){(({}())<>{}({})){((<>))}{}{}}
Outputs 0\n
(literal newline) for truthy, and the empty string for falsy.
The idea is to subtract 1 then 2 then 3 all the way up to the input. If you hit 0, then you know this is a triangular number, so you can stop there.
Try it online! (truthy)
Try it online! (falsy)
# 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
}
Here's a 46 byte solution that I found interesting.
{<>(({}())){({}[()]<>{(<({}[()])>)}{}<>)}{}<>}
Outputs 0\n
(literal newline) for truthy, the empty string for falsy.
The idea is to count down from input by consecutive numbers, 1 at a time. E.g. input - (1) - (1,1) - (1,1,1)
. Each time we subtract, if we aren't at 0 yet, we leave an extra value on the stack. That way, if we are at 0 and are still subtracting when we pop we remove the last value on the stack. If the input was a triangular number, we will end exactly at 0, and wont pop the 0.
Try it online! truthy
Try it online! falsy
# 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.
}
1Related, related. – ETHproductions – 2017-05-22T20:27:21.657
Related OEIS sequence – ovs – 2017-05-22T21:14:39.560
Related – Shaggy – 2017-05-22T21:28:46.867
Why didn't you include zero? – Neil – 2017-05-22T21:40:17.377
1@Neil I wanted to minimize the number of possible edge cases, and handling zero is one of them that I felt wasn't too important. Do you think it would have been better if zero needed to be handled? (The Jelly answer currently fails on zero, for instance) – ETHproductions – 2017-05-22T21:44:45.203
Bah, I would have thought programmers would have learned how to count starting at zero by now. – Neil – 2017-05-22T23:05:44.197
So, would an integer which is truthy in our language not count? – Rohan Jhunjhunwala – 2017-05-23T02:24:06.940
@RohanJhunjhunwala There is no requirement that your output values be truthy or falsy, but you can only have two of them. – Ørjan Johansen – 2017-05-23T04:46:16.287
Related – alephalpha – 2017-05-25T02:58:53.987
Related – programmer5000 – 2017-07-19T01:12:02.550
Would this be a valid output for non-triangular numbers? – Oliver – 2018-12-06T19:01:32.723