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!
# Set up the stacks like this: -input
# Output 1 for triangular and 0 for non-triangular
For a full explanation please see Wheat Wizard's answer.
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.
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.