|
-
Dec 5th, 2016, 07:01 PM
#1
Re: Recursion
I think DDay pointed the way to work it out with pen and paper -- or on screen, in his case. Once your exit condition test is in place, work out the result of the function by hand with starting values of n = 2, 3, 4, 5 and so on. You'll soon notice a pattern in the results!
I'm assuming you can't start with n negative, by the way.
BB
-
Dec 5th, 2016, 07:36 PM
#2
Fanatic Member
Re: Recursion
Hello
Is this a homework assign?
I agree with BB you need to start with n=2. f(0)=0 and f(1)=0 by your conditions.
I do not agree with dday9 as to the values.
I believe f(2)=-1, f(3)=-2, f(4)=-3, f(5)=0 and so on.
I wrote code that did not use resursion and believe f(1337)=444
In my experience dday9 is usually right. So... ?
George
-
Dec 5th, 2016, 08:00 PM
#3
Re: Recursion
EDIT: George was right after all, and I was wrong. Reminder to self: never post opinions about code without testing them first! What I mistakenly wrote was this:
George, f(2) evaluates to f(2 - 1) - 1 = f(1) - 1. Since evaluation takes place from left to right, f(1) has to be calculated before the 1 can get subtracted; and it never does get subtracted, because evaluating the function f(1) returns 0 and that's the end of it. This kind of thing happens all the time .
BB
Last edited by boops boops; Dec 10th, 2016 at 06:06 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|