Lets work it through shall we...
5 --> not 1 so Ouput 5 and 15 push 15/f(4) on stack
4 --> not 1 so Output 4 and 10 push 10/f(3) on stack
3 --> not 1 so Output 3 and 6 push 6/f(2) on stack
2 --> not 1 so Output 2 and 3 push 3/f(1) on stack
1 --> is 1 Output 1 return 1
pop 3+f(1) replace f(1) with 1 (that is what we returned)
so return 11
pop 6+f(2) replace return 17
pop 10+f(3) replace return 37
pop 15+f(4) replace return 52
so the function returns 52
You do the other one 🙂
Step 2 iterative:
// Lets do it in php shall we
$recurse = 1;
$num = 0;
function recurse($n) {
for($i = $n; $i > 0; $i--) {
echo "$n ";
$tmpNum = $n*($n+1)/2;
echo "$tmpNum";
$num = $tmpNum + $num;
}// end for
return $num
}// end function
I think that is write but I am running out the door. BTW dont make me do your homework again. 🙂
Hope this Helps!