Fastest method to define whether a number is a triangular number

If n is the mth triangular number, then n = m*(m+1)/2. Solving for m using the quadratic formula:

m = (sqrt(8n+1) - 1) / 2

So n is triangular if and only if 8n+1 is a perfect square. To quickly determine whether a number is a perfect square, see this question: Fastest way to determine if an integer’s square root is an integer.

Note that if 8n+1 is a perfect square, then the numerator in the above formula will always be even, so there's no need to check that it is divisible by 2.


An integer x is triangular exactly if 8x + 1 is a square.