Triangle numbers are :
1,3,6,10,15,21,28.
Somebody tells you a random positive integer of any size. Is there a quick way to work out if it is a triangle number or not (in other words, without going through all of the triangle numbers untill you get to a number as high or higher as the integer you've been told)?
The formula for the nth triangle number is
T(n)=[n(n+1)]/2.
So, a method that relies largely on inspection would be:
Suppose you are given the integer T. Take the square root of twice T, then the 'floor' of that value (round down to the next highest integer). That should be your n, if T is indeed a triangle number.
n = _√(2T)_
Then just check; multiply n by n+1, take half of that sum, and it should be your original number. If it is, then T is the nth triangle number; if not, then T is not a triangle number at all.
I'm sure other, cleaner methods will be found, but that simple method presented itself, and it doesn't violate the rules of the problem.
|
Posted by DJ
on 2003-08-07 15:27:26 |