( ! ) Notice: session_start() [function.session-start]: Server 127.0.0.1 (tcp 21201) failed with: Failed reading line from stream (0) in /var/www/html/common-2.3/SWAT/SWAT.php5 on line 108
Call Stack
#TimeMemoryFunctionLocation
10.000693280{main}( )../discussion.php5:0
20.0011170168include_once( '/var/www/html/interactivate/config/standardIncludes.php5' )../discussion.php5:22
30.120018467920SWAT->startSession( )../standardIncludes.php5:110
40.120118467920session_start ( )../SWAT.php5:108
Interactivate: Recursion

Interactivate


Recursion


Shodor > Interactivate > Discussions > Recursion

Student: Tell me about recursion. Is it the same as iteration or not?

Mentor: Recursion is a special kind of iteration. Here's the idea: With a recursion we are given starting information and a rule for how to use it to get new information. Then we repeat the rule using the new information as though it were the starting information.

Student: So we have a loop? What comes out of the rule goes back into the rule for the next iteration?

Mentor: That's a good way to think about it. Here's a classic example of a recursion which cranks out a sequence of numbers called the Fibonacci Numbers:

Here we have starting information and a rule for generating a new value. The n increases by one each time, so we can ask questions like find the ninth fibonacci number. We are given two starting values since each new value is calculated from the two previous ones. Try generating the ninth number.

Student: Let's see. The first numbers are 1 and 1, as given. The rule says to take the two previous numbers and add them to get the new number, so I would get:

n = 3: 1 + 1 = 2
n = 4: 1 + 2 = 3
n = 5: 2 + 3 = 5
n = 6: 3 + 5 = 8
n = 7: 5 + 8 = 13
n = 8: 8 + 13 = 21
n = 9: 13 + 21 = 34

Mentor: Good! Now let's consider the fractal examples we have seen so far. For fractals, the starting information is called the initiator, the rule for iterating is called the generator:

Fractal Initiator Generator After Several Iterations
Hilbert Curve Straight line segment
Another Hilbert Curve Straight line segment
Kock's Snowflake Triangle
Each Side of Triangle

Student: So in each of these, we have the starting stage and the rule for moving to the next stage.

Mentor: Yes!


a resource from CSERD, a pathway portal of NSDL NSDL CSERD