>> SPEAKER 1: In that last version of sigma, I implemented what I would call
an iterative solution, whereby I used a forward loop to count up all of the
numbers between 1 and m, thereafter returning the sum.
>> But it turns out we can use another technique to implement that same
function, a technique known as recursion.
A recursive function, so to speak, is simply one that calls itself.
Now, in and of itself, that might be a problem.
If a function simply calls itself which calls itself which calls itself,
that process might bot ever end.
But so long as we include a so-called base case, a condition that ensures
that in some situations we don't call ourselves, that process of otherwise
infinite looping should cease.
>> Let's now reimplement sigma as follows.
If n is less than or equal to 0, I'm simply, and somewhat arbitrarily,
going to return 0.
Else what I'm going to do is actually compute sigma for the positive int
that I've been handed.
>> Now, what is sigma of m?
Well, sigma of m is, of course, the sum of 1 up through m.
But if we think about it the other way, it's simply the sum of m plus m
minus 1 plus m minus 2 and so forth, all the way down to 1.
So in that sense, it seems that I could simply return m plus.
>> And then I need m minus 1 plus m minus 2.
But I have a function that can give me precisely that answer, namely
sigma of m minus 1.
>> Now, calling myself in this way doesn't seem like the best idea.
Because if sigma calls sigma which calls sigma which calls sigma, you
would think that this process might not ever end.
But that's why we had the so-called base case at the top of this function.
The if condition that checks if m is less than or equal to 0 I'm not going
to call myself.
I'm instead going to return 0, which in turn is going to be added to the
previous numbers that I've been summing up, thereby stopping this
otherwise infinite process.
>> Let's now see if this new implementation works.
Let's save, compile, and run this program.
Make sigma 1 dot slash sigma 1.
And let's provide it with the same numbers as before.
2, which should hopefully give me 3.
Let's provide it with 3, which should hopefully give me 6.
And let's finally provide it with 50, which indeed gives me 1,275.