How would you teach recursion?

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
to freshman students. I was thinking about giving them an example, such as factorial, or something simple, but how would you explain it?
 

SagaLore

Elite Member
Dec 18, 2001
24,036
21
81
Originally posted by: chuckywang
to freshman students. I was thinking about giving them an example, such as factorial, or something simple, but how would you explain it?

 

kstu

Golden Member
Feb 23, 2004
1,544
31
91
exponentiation example

edit: didnt see the how to part. my professor did a really good job. he explained what the recursive case and the base case were. then drew the stack of method calls and what parameters they were receiving until the base case was reached, then all the return values. on the blackboard, simple and effective.
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Originally posted by: chuckywang
to freshman students. I was thinking about giving them an example, such as factorial, or something simple, but how would you explain it?

All jokes aside... to freshman what? High school students? College students?

I think factorial is something simple enough... but it doesn't explain the "why"... as you can easily compute factorials in a much more straightforward fashion.

There are quite a few math problems that easily lend themselves to be solved via recursion... but damn if I can recall any off the top of my head now...

:eek:
 

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
Originally posted by: MathMan
Originally posted by: chuckywang
to freshman students. I was thinking about giving them an example, such as factorial, or something simple, but how would you explain it?

All jokes aside... to freshman what? High school students? College students?

I think factorial is something simple enough... but it doesn't explain the "why"... as you can easily compute factorials in a much more straightforward fashion.

There are quite a few math problems that easily lend themselves to be solved via recursion... but damn if I can recall any off the top of my head now...

:eek:

Yeah, freshman college students.
 

BlueFlamme

Senior member
Nov 3, 2005
565
0
0
Originally posted by: MathMan
There are quite a few math problems that easily lend themselves to be solved via recursion... but damn if I can recall any off the top of my head now...

Sorting (I can try to find it in my notes when I get home, but I believe there is a really good quick or bubble sort example using a deck of cards)
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
I learned it through factorial. Just a simple program like this:

public int Factorial(int i)
{
int retVal;

if (i = 1)
retVal = 1;
else (i > 1)
retVal i * Factorial(i-1);

return retVal;
}

This introduces them to the base case (i = 1) where the recursion stops. You can then trace it back down the stack for them.
 

skace

Lifer
Jan 23, 2001
14,488
7
81
I believe directory structure is a good case for recursion. Say you want to know how many empty directories exist but you have to also ensure the subdirectories are empty before removing empty directories above them. Or is that too real world?
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
recursion is nice but i dont like the that it uses alot stack space unless its the only clean way to slove a proble.
 

Cristatus

Diamond Member
Oct 13, 2004
3,908
2
81
For some people, you may have to teach what a factorial is first. I did not know what a factorial is, and yet I did some programming, but I can't remember how I was taught it.
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Originally posted by: logic1485
For some people, you may have to teach what a factorial is first. I did not know what a factorial is, and yet I did some programming, but I can't remember how I was taught it.

I think it's safe to assume that most people learn factorials in middle school.
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
Find a recording of this song and play it for them:

:music: "This is the song that never ends,
Yes it goes on and on, my friends.
" :music: