MATLAB: Best way to determine if number is triangular number

triangular numbers

Hello,
I would like to test if an integer is a triangular number, and am wondering what the best way to achieve this is. I am aware that a triangular number is given by
x = n(n+1)/2
And that x is triangular if and only if 8x + 1 is a square.
However, what is the best way to actually test this in Matlab, giving consideration to floating point arithmetic?
I can think of methods like:
check x is an integer find if sqrt(8x + 1) is an integer to some floating point error
but there must be better ways.

Best Answer

istri = @(x) floor(sqrt(8*x+1))==sqrt(8*x+1);
x = 1:20;
idx = istri(x)
x(idx)