previous
|
start
|
next
Triangle Numbers
The technique of expressing a solution to a problem in terms of solution to a smaller version of the same problem is called
recursion
.
There are two key requirements to make that recursion is successful:
Every recursive call must simplify the computation in some way.
The must be special case to handle the simplest computations directly.
Here,
get_area
calls itself again with smaller and smaller width values, eventually reaching a width of 1.
Recursion is not really necessary to solve this problem:
A simple loop:
double area = 0; for (int i = 1; i <= width; i++) area = area + 1;
A formula:
width * (width + 1) / 2
previous
|
start
|
next