# Is this number triangular?

## Challenge

Given a positive integer, determine whether it is a triangular number, and accordingly output one of any two constant, distinct values.

### Definition

A triangular number is a number that can be expressed as the sum of consecutive positive integers, starting at 1. They can also be expressed with the formula n(n + 1) / 2, where n is some positive integer.

## Test cases

Truthy:

1
3
6
10
15
21
55
276
1540
2701
5050
7626
18915
71253
173166
222111
303031
307720
500500
998991


Falsy:

2
4
5
7
8
9
11
16
32
50
290
555
4576
31988
187394
501500
999999


## Rules

• Your entry may be a function or a program.
• You may assume that the input is a positive integer under 106.
• You must pick two constant, distinct outputs to distinguish the two categories.

This is , so the shortest code in bytes in each language wins.

• – ETHproductions May 22 '17 at 20:27
• Related OEIS sequence – ovs May 22 '17 at 21:14
• Related – Shaggy May 22 '17 at 21:28
• Why didn't you include zero? – Neil May 22 '17 at 21:40
• @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 May 22 '17 at 21:44

# 05AB1E (legacy), 4 bytes

ÅTs¢


Try it online!

Explanation:

ÅT     //push all triangle numbers <= (implicit) input
s    //push input onto stack
¢   //count occurrences of input in triangle numbers (i.e. 1 if triangle, 0 if not)


I'm using 05AB1E (legacy) since it pre-dates this challenge, but it works in current 05AB1E as well

# Alchemist, 80 bytes

_->In_n+s
f+b->f+a
f+0b->a
0f+n+a->b
0f+0a+n->n+f
0n+a+s->Out_n
0n+0a+s->Out_"1"


Try it online!

Test cases

Subtracts increasing numbers from the input until it reaches 0, and checks if the counter is zero.

# Gol><>, 12 bytes

I8*P12,X1%zh


6 bytes golfed off, courtesy of JoKing! A codebreakdown will be coming soon!

Try it online!

First version, 18 bytes

I8*P12,X1}:S(-Zh0h


I found a pretty simple formula for solving this, which helped golf this, since we are only looking to see if the number is triangular, rather than calculate it.

Link to wiki where I found the formula!

Code Breakdown!

I8*P              //Multiply the output by eight
12,X          //Get the square root
1}        //Push a value for truthy output and put it on the bottom
:S(-    //Check to see if it is a perfect squareroot, no floating point
Zh0h//If the difference is zero(perfect squareroot) output truthy, otherwise, push a falsey and output!


There is a way to remove 2 bytes, but it outputs the input plus one for truthy, which I don't think is to the specification of "You must pick two constant, distinct outputs to distinguish the two categories".

Try it online!

• You can replace 1}:S(-Zh0h with 1%zh – Jo King Feb 14 at 4:45
• A tip: You don't need to use conditionals like ?ZqQ for value checking. Just transform the values directly into 1 or 0 using comparison operators – Jo King Feb 14 at 5:07
• @JoKing Thank you very much, I have made the changes and it has popped off 6 bytes! – KrystosTheOverlord Feb 14 at 12:10

# Brain-Flak, 62 bytes

(([(({}))])<>){(({}())<>{}({})){(<><>)}{}<>}<>([[]]()()()<>{})


Try it online!

### Explanation

Effectively, this code starts with n and subtracts 1, 2, 3, ... n. If an intermediate result is 0, this is marked by decreasing the size of the left stack.

   (({}))                         duplicate input n
([      ])                       push -n as accumulator
(          <>)                    push -n on other stack as counter

{                            }    while counter is nonzero
({}())                          increment counter
({})                  add stored copy of n (effectively, this adds a counter that starts at 1 instead of -n)
(              )                 push as new accumulator value
{(<><>)}{}       If accumulator is zero, shrink the stack by one
<>     switch back to right stack

<>                                move to left stack
[[]]()()()                     3 minus height of left stack (which is 2 if n is triangular and 3 otherwise)
<>{}                 move back to right stack and pop zero from loop

• I ctrl-F'd for "><>"... Oh, there's already- no wait, that's Brain-Flak? – Esolanging Fruit May 23 '17 at 6:14

## ><>, 30 28 bytes

-2 bytes thanks to lanlock4

Assumes input is on the stack. Outputs either a 1 or 0.

0v
1<~v!?(}:{:,2*+1::+
n={<;


This was fun. I'm new to ><>, and I'd welcome any suggestions for golfing.

This starts with n=1, then continually increments n while n(n-1)/2 is less than the input number. Once the loop terminates, it prints 1 if n(n-1)/2 is equal to the input number, 0 otherwise.

• If you move the +1 to the left of the < on the second line and start at n=0 instead of n=1, you can get rid of both of the spaces, like this. – Not a tree May 23 '17 at 9:27

# 2Col, 8 bytes [non-competing]

*8
+1
Sq


Try it on 2Collide

Braingolf got boring so now I'm making a new language. Link leads to the current 2Col interpreter in TIO, with the above code already inserted. 3rd argument is input.

2Col is a language where each line is a 2 character expression of some form. It's what I like to call an "Accumulator-based" language. It works like Stack-based languages, except the "stack" can only contain a single item.

### Explanation:

        Implicit input to Cell
*8      Multiply Cell by 8
Sq      Return true if Cell is square number
Implicit: Print final line's return value


# CJam, 10

ri8*)_mQ%!


Try it online

It checks if 8*n+1 is divisible by its integer square root.

• Interesting variant of the 8*n+1 method. In general this divisibility check tells if a number is of the form k^2, k^2+k or k^2+2*k, but a number of the form 8*n+1 can only be the first case. – Ørjan Johansen Oct 26 '17 at 19:30

# Pyt, 5 bytes

←Đř△∈


Explanation:

←                 get input
Đ                duplicate input (on stack twice)
ř               push [1,...,input] onto stack
△              calculate the kth triangle number for each element k in the array
∈             check if input is in array of triangle numbers


Longer but faster way, 9 bytes

←Đ2*√⌈ř△∈


Explanation:

←                     get input
Đ                    duplicate input (on stack twice)
2*                  double input
√⌈                ceiling of the square root
ř               push [1,...,value from previous step] onto stack
△              calculate the kth triangle number for each element k in the array
∈             check if input is in array of triangle numbers


# cQuents, 4 bytes

?Z+$ Try it online! ## Explanation ?Z+$
Given input n,
?       Mode query: output true if n is in sequence, false if n is not in sequence
Each term in the sequence equals
Z      Previous term
+                   +
;$ Try it online! ## Explanation ?b$     First program. Checks if the input is triangular.
?       Mode: query. Returns true if the input is in the sequence, and false otherwise.
b$Each item in the sequence is the secondary program, passed the current (1-based) index. ;$      Secondary program. Generates triangular numbers.
;       Mode: series. Calculates the sum of the sequence up to the input.
\$      Each item in the sequence is the current (1-based) index.


# APL(NARS) 12 chars, 24 bytes

{0=1∣√1+8×⍵}


It is done seen(copy) some other solution... it seems {1∣⍵} it is 0 when ⍵ is one int decimal with 0 digits afther the "." test:

  f←{0=1∣√1+8×⍵}
{⍞←{1=f ⍵:' ',⍵⋄⍬}⍵⋄⍬}¨0..200
0  1  3  6  10  15  21  28  36  45  55  66  78  91  105  120  136  153  171  190


but i remember i wrote the solution to this a little +long, some time ago...

# 05AB1E, 4 bytes

ÅTI¢


Try it online!

Explanation:

ÅT     Get list of triangle numbers less than or equal to the implicit input
I¢   Count occurrences of input in list

• In this case the count will give exactly the same output, but just a FYI: there is also a contains builtin: å. :) (PS: There is already an 05AB1E answer using the same approach.) – Kevin Cruijssen Mar 13 at 11:54