Entering 3 into the mystery method will produce an output of 12. The method starts by assigning the int variable n as 3. It then subtracts n by both 1 and 3 using recursion until values < 0 are produced. This procedure, summarized in the figure below, creates six different negative values. It then returns each value as 2, then adds them all together. This results in the output of 12.
3
┌───┴───┐
2 0
┌─┴─┐ ┌─┴─┐
1 -1 -1 -3
┌─┴─┐
0 -2
┌─┴─┐
-1 -3
−Phoenix
Good work, concise explanation. 850 XPs awarded.
ReplyDelete