Let's do a famous exercise, and this might come up as an interview question.
We're going to try and implement Fibonacci numbers.
And Fibonacci numbers are like this.
The first number is zero.
Then the second number is one.
They always start with zero one.
Then this is what happens.
We add zero with one to equal the next number.
We add one with one to equal the next number.
We add one by two to get the next number to three to add the next number five eight to add the next
number, so on and so forth.
As you can see, this grows exponentially.
Well, it grows really high, really fast.
And this sort of numbering actually shows up in nature a lot.
But it's also an interesting problem because these numbers, although at the beginning, grow really,
really slowly, they become larger and larger and larger, and it becomes a problem that can really
hog up the resources.
So this is what I want you to do.
We're going to create a function.
FIB.
That takes a number.
And in here this number is going to be the index number of the Fibonacci.
So, for example, if it was the 20th number, it should have this.
So if I wanted, let's say fib 20.
Here, in that case, I want you to return all these numbers.
Using generator.
So that is one by one until we get to this location.
Now there's two ways of doing this.
You can do with the list, or we can do it with a generator.
And you'll notice that with a generator, it's a lot easier to do than with a list, because eventually
with a list, you're going to run out of resources.
So what I want you to do is to output on here the numbers that you see here all the way until 20.
And if you want, you can keep increasing that number and see what happens.
And as a bonus, you can try implementing this with a list as well.
So it displays in a giant list as well.
Pause the video if you want here.
Otherwise, I'm going to keep going with the answer.
First, what we want to do is say A is going to equal to zero and B is equal to one.
So A and B are going to be the start of the numbers.
Now in here, we're going to use the arrange generator.
To loop.
This number of times that we're going to provide the function.
Now in here.
First I want b to now or eh to now equal whatever B was, which is one and B to be whatever the next
number is.
Remember the rule with Fibonacci?
We add the first two numbers to get the third number.
So we have a B to get the next number, then a B to get the next number.
And then a B to get the next number.
So the first thing I want to do is to actually yield a and say, hey, just give me whatever a is and
then pause the function until the next part.
After that, after the for loop goes to the next part, I'm going to say I want A to equal B.
And I want B to equal A plus B.
Let's see if that makes sense.
I'm saying, hey, return or yield a from this list so it'll be zero at first.
Then I want you to make a equal to one now because we're moving over and then I want B to equal one
because A has turned into one and then the one previously.
So B becomes two.
If we look over here, that's why we want we have A and B here and then we want to move over just one
like this.
So B should still be one.
So what we might want to do here is to actually not modify a.
Until we are able to do A plus B.
So how can we do this?
Well, we can simply say.
Let's say a temporary variable and this temp is going to hold on to whatever a was when we get here,
which is zero, and then we're going to modify A to equal one and then B is going to be instead of A
plus, B will be temp, which is zero.
Plus whatever B was originally, which is one.
I know this can give a confusing.
You might want to pause here and maybe print and see exactly what's happening, but hopefully our function
works now.
If now I do something like for x in fib and give it let's say 100.
And let's just print out the answer.
So print.
X and I run this.
All right, look at that.
I get all my Fibonacci numbers.
Let's do 20 like we saw in the example if I click Run.
I get 4181.
So I'm at 19 because remember our count starts at zero.
So technically here, if we want, we can do 21 here, click run.
And when she got six, seven, six, five, which is what we get here.
Very, very cool.
But you can see how the number increases dramatically.
As we increase to 1000.
Now, if we were to do this in a list, it would look something like this.
Let's copy here.
Now 52 with a list would mean that we have to add a list in here.
And instead of yielding, we'll do result dot append.
And we're going to append whatever the a value is, which is the initial value.
And then we're going to do the same thing we've done before.
So instead of ELT, we're now just appending to this list and at the end we just return.
The list result.
Now, if I return this, let's say fib two and give it 100 and I click run and let's comment out the
original one with the generator.
Well, we should probably print this, shouldn't we?
So let's do print.
And Click Run.
All right.
That's a massive list.
Now, if we were to do 1000 here.
You see that?
It's taking quite a bit of time.
What if we do 10,000?
All right.
It's definitely taking some time.
And you can see over here how hard this is working.
You know what?
I'm going to stop it.
And go back to our generator.
Way.
And let's do 10,000 here.
Let's see how 10,000 looks with the generator.
Well, it's still working hard, but I'm not keeping any of this in memory.
We're taking it one by one, and I can act upon it in our case, just printing it one by one.
Now this is a common interview question.
It's actually a little bit unnecessarily confusing, I think.
But it does demonstrate the power of generators.
You might have to watch this video a couple of times, but hopefully by now you're feeling a little
bit comfortable with using ranges and how we can improve performance using generators.
I'll see you in the next one.
Bye bye.