[SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Orginal solved. Anyone know Recursion?

Post by wtetzner »

wearymemory wrote:
wtetzner wrote:Actually I just though of a visual way to show how it works. Since fib calls two instances of itself, it creates a tree of function calls.

Code: Select all

                fib(4)
              /        \
        fib(2)    +      fib(3)
       /     \        /          \
  fib(0) + fib(1)   fib(1)   +   fib(2) 
    |       |         |          /     \  
    1       1         1       fib(0) + fib(1) 
                                 |       |  
                                 1       1
Hopefully that helps make things more clear.
The fourth Fibonacci number is 3.
Yep, and I'm using a 0-based index, so the fourth Fibonacci number is fib(3) = 3
fib(0) = 1
fib(1) = 1
fib(2) = 2
fib(3) = 3
fib(4) = 5
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Orginal solved. Anyone know Recursion?

Post by wearymemory »

wtetzner wrote:
wearymemory wrote:
wtetzner wrote:Actually I just though of a visual way to show how it works. Since fib calls two instances of itself, it creates a tree of function calls.

Code: Select all

                fib(4)
              /        \
        fib(2)    +      fib(3)
       /     \        /          \
  fib(0) + fib(1)   fib(1)   +   fib(2) 
    |       |         |          /     \  
    1       1         1       fib(0) + fib(1) 
                                 |       |  
                                 1       1
Hopefully that helps make things more clear.
The fourth Fibonacci number is 3.
Yep, and I'm using a 0-based index, so the fourth Fibonacci number is fib(3) = 3
fib(0) = 1
fib(1) = 1
fib(2) = 2
fib(3) = 3
fib(4) = 5
I've always read that f0 = 0, and f1 = 1, are the two seeds to the Fibonacci Sequence.
Also, the OP's code returns 1 if n is less than 3. I guess that lead me to believe that your diagram was wrong. Sorry 'bout that.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Orginal solved. Anyone know Recursion?

Post by wtetzner »

wearymemory wrote:
wtetzner wrote:
wearymemory wrote:
wtetzner wrote:Actually I just though of a visual way to show how it works. Since fib calls two instances of itself, it creates a tree of function calls.

Code: Select all

                fib(4)
              /        \
        fib(2)    +      fib(3)
       /     \        /          \
  fib(0) + fib(1)   fib(1)   +   fib(2) 
    |       |         |          /     \  
    1       1         1       fib(0) + fib(1) 
                                 |       |  
                                 1       1
Hopefully that helps make things more clear.
The fourth Fibonacci number is 3.
Yep, and I'm using a 0-based index, so the fourth Fibonacci number is fib(3) = 3
fib(0) = 1
fib(1) = 1
fib(2) = 2
fib(3) = 3
fib(4) = 5
I've always read that f0 = 0, and f1 = 1, are the two seeds to the Fibonacci Sequence.
Also, the OP's code returns 1 if n is less than 3. I guess that lead me to believe that your diagram was wrong. Sorry 'bout that.
http://en.wikipedia.org/wiki/Fibonacci_number
Wikipedia wrote:By definition, the first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.
I've read that it starts with two ones, but apparently it's been defined both ways.
In any case, hopefully the diagram will make it easier to understand recursion.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Orginal solved. Anyone know Recursion?

Post by Ginto8 »

wtetzner wrote:Actually I just though of a visual way to show how it works. Since fib calls two instances of itself, it creates a tree of function calls.

Code: Select all

                fib(4)
              /        \
        fib(2)    +      fib(3)
       /     \        /          \
  fib(0) + fib(1)   fib(1)   +   fib(2) 
    |       |         |          /     \  
    1       1         1       fib(0) + fib(1) 
                                 |       |  
                                 1       1
Hopefully that helps make things more clear.
you forgot to add a check for whether n < 3...

And to the original poster(jtst1), this saying sums up recursion: "In order to understand recursion, you must first understand recursion". :lol:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Orginal solved. Anyone know Recursion?

Post by wtetzner »

Ginto8 wrote:you forgot to add a check for whether n < 3...
What? Both fib(0) and fib(1) evaluate to 1 in the diagram.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Orginal solved. Anyone know Recursion?

Post by avansc »

wtetzner wrote:The Fibonacci sequence is defined as:

Code: Select all

fib(0) = 1
fib(1) = 1
fib(n) = (n - 2) + (n - 1) for n > 1
(Note that the above is not code, just the mathematical definition.)

What this says is that fib(0) returns 1, fib(1) returns 1, and for every integer n that is above 1, fib(n) returns the sum of the previous two values in the sequence.
So let's compute fib(2).

Code: Select all

Now, fib(n) = fib(n - 2) + fib(n - 1)
Then fib(2) = fib(2 - 2) + fib(2 - 1)
So   fib(2) = fib(0) + fib(1)
     fib(2) = 1 + 1, since fib(0) = 1 and fib(1) = 1
So when you write the function in code, you need to check if n = 0 or if n = 1. These are called the base cases or termination conditions. That's because they are not defined in terms of fib, so when you get a value n that is 0 or 1, it stops the recursion.
Everything else is defined in terms of fib.

Say you call fib with n = 4.
So fib(4) = fib(4 - 2) + fib(4 - 1)
Then fib(4) = fib(2) + fib(3)
Now fib(2) and fib(3) need to be computed in order to find fib(4).
So we compute fib(2):
fib(2) = fib(2 - 2) + fib(2 - 1)
fib(2) = fib(0) + fib(1)
So now we need to compute fib(0) and fib(1) to find fib(2)
But both fib(0) and fib(1) return 1. These are the base cases, since they don't call fib again.
So now we can finish computing fib(2).
fib(2) = fib(0) + fib(1)
fib(2) = 1 + 1
fib(2) = 2
Now we need fib(3) to finish computing fib(4), which we started computing above.
So fib(3) = fib(3 - 2) + fib(3 - 1)
fib(3) = fib(1) + fib(2)
Now we need to compute fib(1) and fib(2) to find fib(3).
fib(1) is a base case, and returns 1.
Computing fib(2) is the same as fib(2) above, which we discovered returns 2.
So fib(3) = fib(1) + fib(2)
fib(3) = 1 + 2
fib(3) = 3
Now we know both fib(2) and fib(3), so we can finish computing fib(4)
fib(4) = fib(2) + fib(3)
fib(4) = 2 + 3
fib(4) = 5

Hopefully I wasn't too unclear. The example was kind of long, but if you follow each step, you should be able to understand the process.
is it just me... or does this look almost exact to what i wrote....
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Orginal solved. Anyone know Recursion?

Post by dandymcgee »

avansc wrote:
wtetzner wrote:The Fibonacci sequence is defined as:
is it just me... or does this look almost exact to what i wrote....
Well, you are both trying to explain the same thing. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Orginal solved. Anyone know Recursion?

Post by jtst1 »

Hey everyone, I didn't expect this many people to reply lol. I understand it now basically.
"In order to understand recursion, you must first understand recursion"
rofl yeah. Can i ask why it has to have n < 3?
When One learns to Love, One must bear the risk of Hatred.
User avatar
aamesxdavid
ES Beta Backer
ES Beta Backer
Posts: 347
Joined: Wed Jan 07, 2009 8:49 pm
Location: Bellevue, WA
Contact:

Re: Orginal solved. Anyone know Recursion?

Post by aamesxdavid »

jtst1 wrote:Can i ask why it has to have n < 3?
Because the first two numbers have to be listed as "1".

That's how the whole sequence starts, and without putting fib(1) and fib(2) as "1", the program has nothing to go on for adding the rest of the numbers up. Is it this one?

Code: Select all

int fib(int n);

int main()
{
    int n, answer;
    cout << "Enter number to find: ";
    cin >> n;

    cout << "\n\n";

    answer = fib(n);

    cout << answer << " is the " << n << "th Fibonacci number.\n";

    return 0;
}

int fib(int n)
{
    cout << "Processing fib(" << n << ")... ";

    if (n < 3)
    {
        cout << "Return 1!\n";
        return (1);
    }
    else
    {
        cout << "Call fib(" << n-2 << ") and fib (" << n-1 << "). \n";
        return(fib(n-2) + fib(n-1));
    }
}
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Orginal solved. Anyone know Recursion?

Post by jtst1 »

aamesxdavid wrote:
jtst1 wrote:Can i ask why it has to have n < 3?
Because the first two numbers have to be listed as "1".

That's how the whole sequence starts, and without putting fib(1) and fib(2) as "1", the program has nothing to go on for adding the rest of the numbers up. Is it this one?

Code: Select all

int fib(int n);

int main()
{
    int n, answer;
    cout << "Enter number to find: ";
    cin >> n;

    cout << "\n\n";

    answer = fib(n);

    cout << answer << " is the " << n << "th Fibonacci number.\n";

    return 0;
}

int fib(int n)
{
    cout << "Processing fib(" << n << ")... ";

    if (n < 3)
    {
        cout << "Return 1!\n";
        return (1);
    }
    else
    {
        cout << "Call fib(" << n-2 << ") and fib (" << n-1 << "). \n";
        return(fib(n-2) + fib(n-1));
    }
}
Yeah that was the code.
When One learns to Love, One must bear the risk of Hatred.
User avatar
RoJaMi
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Fri Aug 14, 2009 2:23 pm

Re: [SOLVED]

Post by RoJaMi »

SO JoTaSt, have you figured out your problem?
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: [SOLVED]

Post by jtst1 »

It's jtst1, and yes if you read on up.
When One learns to Love, One must bear the risk of Hatred.
Post Reply