Confusion with a while statement evaluating if a number is triangular
<p>I've been having troubles trying to figure out how to solve a function. I've been assigned the development of a little programme which tells if a number is "triangular" (a number is triangular when the addition of certain consecutive numbers in the [1,n] interval is n. Following the definition, the number 10 is triangular, because in the [1,10] interval, 1+2+3+4=10).</p>

<p>I've coded this so far:</p>

<pre><code>class TriangularNumber{

  boolean numTriangular(int n) {
    boolean triangular = false;
    int i = n;
    while(n&gt;=0 &amp;&amp; triangular){
    //UE06 is a class which contains the function "f0", which makes the addition of all the numbers in a determined interval
    UE06 p = new UE06();
      if ((p.f0(1, i))==n)
        triangular = true;
      else
        i=i-1;
    }
    return triangular;
  }

boolean testTriangular = numTriangular(10) == true &amp;&amp; 
  numTriangular(7) == false &amp;&amp; 
  numTriangular(6) == true;

  public static void main(String[] args){

    TriangularNumber p = new TriangularNumber();
    System.out.println("testTriangular = " + p.testTriangular);
   } 
}
</code></pre>

<p>According to those boolean tests I made, the function is wrong. As I see the function, it goes like this: </p>

<ol>
<li>I state that the input number in the initial state isn't triangular
(triangular=false) and i=n (determining the interval [1,i] where the
function is going to be evaluated</li>
<li>While n is greater or equals 0 and the number isn't triangular, the
loop starts</li>
<li>The loop goes like this: if the addition of all the numbers in the
[1,i] interval is n, the number is triangular, causing the loop to
end. If that statement is false, i goes from i to (i-1), starting
the loop again with that particular interval, and so on till the
addition is n.</li>
</ol>

<p>I can't spot the error in my "algorithm", any advice?</p>

<p>Thanks!</p>
