how can I solve Pseudocode + function
if condition of function f开发者_Go百科 :
function f(x : integer) : integer;
begin
if x = 1 then
f = 0
else
f = x * f(x - 1) + x^2
end;
find value of f(4)
show me the step by step for calculate please
sorry for my poor english..
In a real language (Python) your code would be written as follows:
def f(x):
return 0 if x == 1 else x * f(x - 1) + pow(x, 2)
Going Down
Lets assume we start with x=4
f(4) = 4 * f(3) + 16
So we nee to evaluate f(3)
f(3) = 3 * f(2) + 9
and then evaluate f(2)
f(2) = 2 * f(1) + 4
Again we need f(1)
f(1) = 0
Coming Back Up
Now we have all the values we need we can go back up the stack of expressions:
f(2) = 2 * 0 + 4 = 4
f(3) = 3 * 4 + 9 = 21
f(4) = 4 * 21 + 16 = 100
精彩评论