C
C#14mo ago
randomokay

❔ homework

hey im trying to understand this code
No description
18 Replies
randomokay
randomokayOP14mo ago
with this as input f(f(4,2), 1) it stops running after y == 0 or y == x right
McBacon
McBacon14mo ago
Yes, it's a recursive function, so it calls itself until the y is eqal to 0 or y is equal to x. I recommend to write a tree of calls on a paper
randomokay
randomokayOP14mo ago
i did i have a question about when f(4,2) is done though since the input is x and y with y being 1 for example and x is 4,2? how does that work
McBacon
McBacon14mo ago
Okay so at first you go into the most nested call, which is f(4,2) and analyze it. The f(4,2) returns f(3,2) + f(3,1) and so on and so on - the result of this recursion which I believe is 6 and then you analise f(6, 1).
randomokay
randomokayOP14mo ago
ye i have 6 aswell so after f(4,2) it just takes 6,1 then it will continue again?
McBacon
McBacon14mo ago
Yes - you got f(6, 1) right now And you need to perform the same analysis
randomokay
randomokayOP14mo ago
ye its just that in the answers they said it was 6 but thats a bit weird they didnt explain the answer though
McBacon
McBacon14mo ago
Hmmm strange thing, let me check it on my paper Okay the answer is for sure 6 - I've incorporated it to the Raider and run the function in a programm, but now we need to answer why 😄
randomokay
randomokayOP14mo ago
ah oke
McBacon
McBacon14mo ago
Ah The answer is 6 Because f(6,1) is equal to 6 😄
randomokay
randomokayOP14mo ago
i wrote it down if u do 6,1 the outcome is also 6
McBacon
McBacon14mo ago
Yes I was a bit confused But yeah That's allright
randomokay
randomokayOP14mo ago
so it doesnt take the first part into account
McBacon
McBacon14mo ago
It takes it as an argument to a new call so It would be like invoking f(6, 1) by hand but here you calculate the x argument with that function also you could do even f(f(4,2), f(0,0) it would produce the same outcome f(f(4,2), f(0,0)) *
randomokay
randomokayOP14mo ago
i think i get it thanks for the help
McBacon
McBacon14mo ago
You're welcome If you got any more questions feel free to attack up
Isaaaak
Isaaaak14mo ago
This question made me realize that I suck at recursive stuff and need to learn it better
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server