previous | start | next

## Triangle Numbers

- If the width of the triangle is 1, then the triangle has an area of 1.

  >     []

  >     int Triangle::get_area()
  >     {
  >        if (width = = 1) return 1;
  >        . . .
  >     }

- To deal with the general case, consider this picture.

  >     []
  >     [][]
  >     [][][]
  >     [][][][]

- We think of area of the larger triangle as

  >     smaller_area + width

- To find the smaller area, we make a smaller triangle!

  >     int Triangle::get_area()
  >     {
  >        if (width == 1) return 1;
  >        Triangle smaller_triangle(width - 1);
  >        int smaller_area = smaller_triangle.get_area();
  >        return smaller_area + width;
  >     }

previous | start | next