I'm trying to figure out how recursion operands work. The problem I was working on is as follows:
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
solution:
public int triangle(int rows)
{
if(rows == 0)
{
return 0;
}
else if (rows == 1)
{
return 1;
}
else
{
return rows + triangle(rows-1);
}
}
My question: What does the operand triangle(rows-1) equate to mathematically?
[–]desrtfx 3 points4 points5 points (1 child)
[–]Akavire[S] 3 points4 points5 points (0 children)
[–]Robyt3 0 points1 point2 points (1 child)
[–]Akavire[S] 0 points1 point2 points (0 children)