go to previous page   go to home page   go to next page hear noise highlighting

Answer:

Of course.


Iterative Triangle

Here is an iterative method that calculates Triangle

public int Triangle( int N )
{
  int totalPins = 0;
  for ( int row=1; row <= N; row++ )
    totalPins += row;
    
  return totalPins;
}

You might prefer this to the recursive version because iteration is familiar. But it is harder to see how the interative version implements the math-like definition.


QUESTION 21:

Is there a formula that gives Triangle(N) immediately?


go to previous page   go to home page   go to next page