1
14:440:127– Introduction to Computers for Engineers
Notes for Lecture 11
Rutgers University, Fall 2010
Instructor Brenda V. Cortez
1
Recursion
There’s a very interesting technique in computer programming called
recursion
, in which you write
a
function that calls a variation of itself to get the answer
. Eventually, this string of functions call
ing itself stops at some
base case
, and the answers work their way back up. Of course, in order to
understand recursion, you must understand recursion. (haha! Get it?! Recursive Joke!).
As a first example, let’s consider calculating a factorial.
If you want to calculate
n!
(
n
facto
rial), you could write this as
n!
= n
*
(n
1)!
.
In other words,
“n factorial”
is actually just
n
times “n1 factorial”
. Thus, we’ve defined the factorial of
n
using factorial again. In other
words, “to calculate the factorial (of
n
), calculate the factorial (of
n1
).”
Eventually, you need to get down to a base case.
What should your base case be?
Well,
1!
is
1
, so make that your base case. So how do you write this as Matlab code?
Often, you’ll include
the base cases first
.
function y = myfactorial(x)
if(x==1)
y = 1;
else
y = x * myfactorial(x1);
end
function y = myfactorial(x)
y = 1;
for z = x:1:1
y = y*z;
end
Why do we use recursion? Well, it’s sometimes very simple to define a problem recursively, particu
larly when creating
distributed algorithms in networks
, or performing searches, or that sort of thing.
Pretty much all of the problems we’re showing you in lecture can be done about as easily with
iterative (performing something over and over again) algorithms using loops, but not all algorithms
can easily be written iteratively.
Let’s now look at how you can use recursion to calculate a term in the Fibonacci sequence. Recall
that the first two terms of the Fibonacci sequence are 1, and all other terms are the sum of the
previous two terms. We’ll make
F(1) = 1
and
F(2) = 1
our base cases.
Those are the cases
where we trivially know the answer!
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2
If you wanted to find the fifth term of the Fibonacci sequence,
F(5)
, you could say that:
F(5) = F(4) + F(3).
So now, we need to know what F(4) and F(3) are.
F(4) = F(3) + F(2).
So now, we need to know what F(3) is. F(2) is 1, by the base case.
F(3) = F(2) + F(1).
Nice, F(2) = 1, and F(1) = 1, by our base cases.
Thus, we work backwards:
F(3) = F(2) + F(1) = 1 + 1 = 2.
F(4) = F(3) + F(2) = 2 + 1 = 3.
F(5) = F(4) + F(3) = 3 + 2 = 5.
In English, we’ve essentially said that the “nth fibonacci number is the (
n1
)st fibonacci num
ber plus the (
n2
)nd fibonacci number.”
Again, we’ve defined a fibonacci number in terms of...
well, other fibonacci numbers.
We keep calling our Fibonacci number function for smaller and
smaller numbers until we got to the base cases. We define our base cases as the first and second
fibonacci number  they’re both
1
, which we know pretty trivially. Then, we worked backwards,
filling in the blanks. So how do you write this as Matlab code? Again, include the base cases first.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Finch
 Computer Programming, Recursion, Binary numeral system, base case, Rutgers University

Click to edit the document details