How to Write a Fast and Iterative Solution to the Fibonacci Problem
Hey guys, how are you doing? This is Kazi so in this video, I'm going to talk about how to write a very fast and iterative solution to the Fibonacci problem. Now, if you're at a job interview and they don't care whether you write a recursive function or a regular function and they give you okay go ahead and write Fibonacci please don't write it recursively because that is the naive solution of this problem. The way you want to write it is the fast iterative way that I'd like to show you now. This way is also you know you can see it on Stack Overflow and it's one of the most popular and the best ways to write it so it's very useful if you learn this initially you might have to memorize it but if you practice it enough times you'll start getting the hang of how this solution works.
So, let's get started. I'm going to say I'm going to define a Fibonacci of n okay and we're gonna say that in the start I'm gonna have some variable called a and B and I'm gonna set their values to 0 and 1. Remember those two values are given right 0 and 1 and then every valley after that is just the sum of the previous two so we're gonna use python's multiple assignment operation here and the swapping value to you write a very elegant solution it's very pythonic. So, we're going to use some beautiful Python code and some Python built-in tricks to write this.
So now what I want to do is I want to run my I want to write a for loop that basically keeps adding values keep summing the previous two terms so my code is going to run the same number of times as the term that you're looking for. So, if you're looking for the fifth term is gonna run five times. So here's how I'm gonna write my loop and I'm going to say is be a comma B is equal to B comma and this takes it makes use of some of python's built-in elegance you know the assignment operators and the swapping of values you can't really do this in another language you're gonna need something called a temporary variable if you're using C or Java or something like that. So, here what I'm effectively saying is B is now the previous term and the new term is the sum of the previous two terms okay so if we wanted to make a run through this we would effectively say okay I come down here B is 1 and a plus B in this case would be 0 plus 1 and you will get 1 okay and that's a third term. So, this is gonna always be the third term and this is always going to be the second to last term.
Okay so a plus B this is the last last term and this B is the second to last term alright and at the end I'm just gonna say a return a. Remember a is what we need to return at the end now I'm gonna run this solution okay okay so now I'm going to run my code here all right so I'm gonna say Python and basics that py oh whoops it's not going to return anything I gotta add a print print statement here I'm gonna say oops Fibonacci of let's say 2 alright I got one that's good so let me just write the Fibonacci terms here so you guys can recall how they work and each one is some of the previous to do so here three plus five gives it gives you eight right here zero comma one so here zero plus one gives you one one plus two gives you three two plus three gives you five so each last term is a sum of the previous two and here you're gonna see that my code is gonna work. So, I'm gonna go and ask for the fifth term what's the fifth term this is the first second I'm sorry zeroth first second third fourth and fifth so fifth should give me five so let's run it and oh I have to save it first almost run it again and I'm getting back five okay and if I said give me the sixth term is gonna give me back eight alright so my code is working here and you can see this is a very simple solution it's very beautiful it's very pythonic and this is the way that you should write this solution when you're at a job interview UK or if you're applying for programming boot camps or whatnot because a lot of the times you're gonna get a question like this they're gonna try to understand if not only you know how to write functions and algorithms but how efficiently you can write them so the iterative way is the way that I think you should approach this thank you for watching and I'll see you guys in the next video.
"WEBVTTKind: captionsLanguage: enhey guys how are you doing this is Kazi so in this video I'm going to talk about how to write a very fast and iterative solution to the Fibonacci problem now if you're at a job interview and they don't care whether you write a recursive function or a regular function and they give you okay go ahead and write Fibonacci please don't write it recursively because that is the naive solution of this problem the way you want to write it is the fast iterative way that I'd like to show you now this way is also you know you can see it on Stack Overflow and it's one of the most popular and the best ways to write it so it's very useful if you learn this initially you might have to memorize it but if you practice it enough times you'll start getting the hang of how this solution works so let's get started so I'm going to say I'm gonna define a Fibonacci of n okay and we're gonna say that in the start I'm gonna have some variable called a and B and I'm gonna set their values to 0 and 1 so remember those two values are given right 0 and 1 and then every valley after that is just the sum of the previous two so we're gonna use pythons multiple assignment operation here and the swapping value to you write a very elegant solution it's very pythonic so we're going to use some beautiful Python code and some Python built-in tricks to write this ok so now what I want to do is I want to run my I want to write a for loop that basically keeps adding values keep summing the previous two terms so my code is going to run the same number of times as the term that you're looking for so if you're looking for the fifth term is gonna run five times so here I'm gonna write my loop and I'm going to say is be a comma B is equal to B comma and here's the cool way I'm going show you so let's move this so this takes it makes use of some of pythons built-in elegance you know the assignment operators and the swapping of the values you can't really do this in another language you're gonna need something called a temporary variable if you're using C or Java or something like that so here what I'm effectively saying is B is now the previous term and the new term is the sum of the previous two terms okay so if we wanted to make a run through this we would effectively say okay I come down here B is 1 and a plus B in this case would be 0 plus 1 and you will get 1 okay and that's a third term so this is gonna always be the third term and this is always going to be the second to last term okay so a plus B this is the last last term and this B is the second to last term all right and at the end I'm just gonna say a return a so remember a is what we need to return at the end now I'm gonna run this solution okay okay so now I'm going to run my code here all right so I'm gonna say Python and basics that py oh whoops it's not going to return anything I gotta add a print print statement here I'm gonna say oops Fibonacci of let's say 2 all right I got one that's good so let me just write the Fibonacci terms here so you guys can recall how they work and each one is some of the previous to do so here three plus five gives it gives you eight right here zero comma one so here zero plus one gives you one one plus two gives you three two plus three gives you five so each last term is a sum of the previous two and here you're gonna see that my code is gonna work so I'm gonna go and ask for the fifth term what's the fifth term this is the first second I'm sorry zeroth first second third fourth and fifth so fifth should give me five so let's run it and oh I have to save it first almost run it again and I'm getting back five okay and if I said give me the sixth term is gonna give me back eight all right so my code is working here and you can see this is a very simple solution it's very beautiful it's very pythonic and this is the way that you should write this solution when you're at a job interrview UK or if you're applying for programming boot camps or whatnot because a lot of the times you're gonna get a question like this they're gonna try to understand if not only you know how to write functions and algorithms but how efficiently you can write them so the iterative way is the way that I think you should approach this thank you for watching and I'll see you guys in the next videohey guys how are you doing this is Kazi so in this video I'm going to talk about how to write a very fast and iterative solution to the Fibonacci problem now if you're at a job interview and they don't care whether you write a recursive function or a regular function and they give you okay go ahead and write Fibonacci please don't write it recursively because that is the naive solution of this problem the way you want to write it is the fast iterative way that I'd like to show you now this way is also you know you can see it on Stack Overflow and it's one of the most popular and the best ways to write it so it's very useful if you learn this initially you might have to memorize it but if you practice it enough times you'll start getting the hang of how this solution works so let's get started so I'm going to say I'm gonna define a Fibonacci of n okay and we're gonna say that in the start I'm gonna have some variable called a and B and I'm gonna set their values to 0 and 1 so remember those two values are given right 0 and 1 and then every valley after that is just the sum of the previous two so we're gonna use pythons multiple assignment operation here and the swapping value to you write a very elegant solution it's very pythonic so we're going to use some beautiful Python code and some Python built-in tricks to write this ok so now what I want to do is I want to run my I want to write a for loop that basically keeps adding values keep summing the previous two terms so my code is going to run the same number of times as the term that you're looking for so if you're looking for the fifth term is gonna run five times so here I'm gonna write my loop and I'm going to say is be a comma B is equal to B comma and here's the cool way I'm going show you so let's move this so this takes it makes use of some of pythons built-in elegance you know the assignment operators and the swapping of the values you can't really do this in another language you're gonna need something called a temporary variable if you're using C or Java or something like that so here what I'm effectively saying is B is now the previous term and the new term is the sum of the previous two terms okay so if we wanted to make a run through this we would effectively say okay I come down here B is 1 and a plus B in this case would be 0 plus 1 and you will get 1 okay and that's a third term so this is gonna always be the third term and this is always going to be the second to last term okay so a plus B this is the last last term and this B is the second to last term all right and at the end I'm just gonna say a return a so remember a is what we need to return at the end now I'm gonna run this solution okay okay so now I'm going to run my code here all right so I'm gonna say Python and basics that py oh whoops it's not going to return anything I gotta add a print print statement here I'm gonna say oops Fibonacci of let's say 2 all right I got one that's good so let me just write the Fibonacci terms here so you guys can recall how they work and each one is some of the previous to do so here three plus five gives it gives you eight right here zero comma one so here zero plus one gives you one one plus two gives you three two plus three gives you five so each last term is a sum of the previous two and here you're gonna see that my code is gonna work so I'm gonna go and ask for the fifth term what's the fifth term this is the first second I'm sorry zeroth first second third fourth and fifth so fifth should give me five so let's run it and oh I have to save it first almost run it again and I'm getting back five okay and if I said give me the sixth term is gonna give me back eight all right so my code is working here and you can see this is a very simple solution it's very beautiful it's very pythonic and this is the way that you should write this solution when you're at a job interrview UK or if you're applying for programming boot camps or whatnot because a lot of the times you're gonna get a question like this they're gonna try to understand if not only you know how to write functions and algorithms but how efficiently you can write them so the iterative way is the way that I think you should approach this thank you for watching and I'll see you guys in the next video\n"