 What's up guys, Mike the Coder here. Today we are going to go over binary exponentiation. So what is binary exponentiation? Sometimes you need to calculate a number to the power of something to a very large number, right? But sometimes you get overflow and sometimes it's really slow to do it. So this is a way to help you to solve this problem of calculating a number raised to a large power. Okay, so the first thing you need to know, remember multiplication? So if I do x times y times z, right? Remember x times y times z, if I change the order of them, parentheses evaluate which one's first, it's still the same number, right? So if x and y and z are three different numbers, right? And if I change the order of the way I'm multiplying them, it's still the same thing, okay? So that's the first thing you have to remember, okay? Because that's just how it works. So if I change the order of multiplication, it's still the same number when you're evaluating, okay? So now what we're gonna do is that we are going to calculate some number, let's say a, raised to a large power, right? I don't know, like b or something. This very large power, right? This well, very large power. Okay, so what is the first thing you need to understand? If you take a number and you raise it to a very large power, how do you calculate this? Well, what you could do normally is you could just take this number and multiply it by itself, b times, right? b times, b times. And why does this work? Because anything raised to the power, you couldn't just multiply by itself, b times, right? That's the definition of how power works. Like if I raise two to the third, it's just two times, two times, two, right? And you could just evaluate that, right? Problem is that this is pretty slow because if you just evaluate this many number times, it's pretty slow. There's a faster way to do this. So first of all, this, just evaluating this, would take o of n time, right? So if I had n numbers, right? If I had n numbers for a to the n power, this is going to be o of n time, complexity, right? Because it would take this min number of, is that numbers would take this much number of time in order to calculate that many number of times in order to go it, okay? So there's a way to do this instead. Now we're going to look at a different way to evaluate the numbers using n log of n time, okay? We're going to try to do this in log of n time, okay? Okay, so let's say I have a number a and it's raised to the b power, b plus c, okay? B plus c, okay? So this is the definition of multiplication for laws of exponents, right? Like if I take anything to the power and this is like a number, right? I basically, what I'm doing is I could split whatever number into two numbers, right? Into two numbers, like if I have a very large exponent, I could just split them up and if I could split them up into two different computations, they'll be better, right? It's better if I could split them up, evaluate it once, then I don't have to evaluate the second one again, right? So if I have a very large exponent raised to a very large exponent, so if I have a raised to a very large exponent, right? If I could be basically split up into two multiplications, b plus c, right? And that's just how evaluating it, okay? Now let's say instead of having like a super duper large number, let's say I have a to the two b, right? a to the two b, I could actually split this into a to the b times a to the b, right? That's splitting up into two b plus b and this is actually equal to a to the b squared, right? a to the b, b times b is equal to a to the b squared. So if I actually know the calculation of one of these values of a to the b power, one of these values, I don't have to recalculate this over and over again, right? I don't have to actually recompute these values over and over again. I could just take the previous value and just multiply it by itself, right? I don't have to completely recompute a times a times a b times. If I just have one of the exponents on the exponents, I could just split them up into two groups and then I'll be able to just calculate it once and then I could just square it. So that's what this binary exponentiation is doing. Basically, if we have some power, some number raised to a power, we could actually split it. If it's an even number, we could actually split them into two different groups, right? And then you could actually just calculate one of them once and then you could just square it so that you don't actually have to multiply, multiply it by itself n times. It would be log base two of n because you're splitting it into two groups every single time and you only have that number of calculations. You're not actually doing it over and over and over again. Okay, so let's go over an example. So let's say I have three to the 13. So what do I wanna do? I wanna basically want to split this exponent by the powers of two. So anything that has like split this exponent to its powers of two. So then basically I would be able to just calculate one of the values once, one of the groups once and then just square it. So if I could have this exponent, I could split into powers of two, then that'll be fine. So how would you split this in 13 into its powers of two? Well, 13 base 10, right? Is actually in binary, it's one, one, zero, one, I think, one, one, zero, one, yeah. So zero, two, zero, two, one, two, two, two, three. Okay, yeah. Two, zero, two, one, two, two, okay. Yeah, so this is in binary. The binary of 13 to the 10 is one, one, zero, one. So if you split this number into its binary equivalent, right? It would actually tell you the powers of two that you could do. So once you're given this binary number of one, one, zero, one, you could actually get, this is actually equal to two to the third. So how I'm getting this is because this is zero power, first power, second, third, right? This is the zero power, two to the zero, two to the one, two to the two, two to the three. So 13 to the base 10 is actually equal to one times two to the third, plus one times two to the second, plus one times two to the first, this is actually zero, zero times two to the first, plus one times two to the zero, okay? So that's what this, if you convert this into binary in its equivalent, these would be what the powers are, right? Two to the third, this is second, first, zero, okay? So now you're given this, now that you know after splitting into its binary equivalence, you could just now use these values as a way to, these values as a way to split your exponent. So three to the 13 is actually equal to three to the eight, right? Cause two to the third is eight, three to the eight, times, times, whoops, three to the fourth, times three to the first, okay? So this is like a 12 plus one is, yeah. 12 plus one is 13. So yeah, these would be your exponents, eight, four, one. So this would be your exponents of what the values you want, eight, four, one, okay? So now that I have the exponents, eight, four, and one, what am I gonna do to basically stop doing like calculations so over and over again? Okay, so what I'm gonna do is I'm going to actually label three to the first, three to the second, three to the third, no, not three to the third, about three to the fourth, and three to the eighth, okay? So what is three to the first power? That's just three, okay? So now given this three, I could just square it. So if I take three square, three square, it's just gonna be nine, right? So that's three squares, that's nine. So because I know this power, three to the second, three to the fourth, what is three to the fourth? That's just three to the second squared. So remember, three to the fourth is just three to the second squared, right? So this is actually just equals a nine square, which is gonna equal to 81. So I don't have to recalculate over and over again, right? Okay, so now that I have 81, how do I recalculate three to the eighth? Well, three to the eighth is actually three to the fourth, squared, right? Because we could just take this previous value and then square it, right? Three to the fourth, laws of exponents. So this is actually just equal to 81 squared, which I don't feel like calculating that, so I'm just gonna use my trusty calculator. What is 81 squared? 81 times 81. Can I do that? Yeah, okay, that's six, five, six, one. So that's six, five, six, one. So now that I have these, I have these values, right? I could just calculate what three to the 13 is. So three to the eighth is six, five, six, one. So that's just six, five, six, one, right? Because I just calculated this. And now we're gonna just take this and we're gonna multiply three to the fourth, which is gonna be 81. And we're gonna multiply by three to the first, which is three. So the amount of computations I took was way less because I didn't have to calculate three times three, times three, times three, times three, times three, times three, 13 times. So how many operations did I actually do? So if you were to count it, it would actually just be log base two of n, where n is 13. So like if we actually just count it, I don't feel like counting how many I did. But yeah, basically your answer would just be six, five, six, one, times 83, times three. And that's your final operations, right? This would be your final answer. I don't feel like putting in my calculator to do this. So yeah, so this is basically the gist of how you would do this by hand and how you do this by the computer. So I'm going to explain how to do it by code now, okay? Okay guys, so how would you take this, do this in code, okay? So let's say I want to get a to the b power, right? Take a power of a and I raise it to b. Okay, so how would you do this in code? First of all, I need a result. We're just going to be one, because this is going to be the answer that I'm going to return, okay? I need a result with my answer of a raised to b power. Okay, so what am I going to do? First, remember we have our exponent b, right? So we have our exponent b and we have to basically do this certain number of times of calculations, right? Every time we're actually going to divide by two. Remember our exponent b? Because of, that's how you convert into binary, right? The binary one, one, zero, one, okay? So what I have to check first is while b is greater than zero. And the reason why I'm checking this first is because we're reducing our exponent every single time. So that's why we have to check while b is greater than zero. Okay, each time this happens, I'm going to shift right by one, which is just dividing it by two. So we could just do while shift right, which equals to one. I think that's the right, that's the, yeah, that's the right way to do it. Okay, this is just dividing by two, pretty much. Okay, so while this is occurring, what I'm going to do is I need to check if my current power of b, right, is if it's odd or even, okay? So if it's odd, so I need to check if it's odd. If it's odd, then I'm just going to multiply, multiply my result by a once, right? Because the reason why I need to do that is because if it's odd, then I just multiply it by itself. If it's even, I just square it, right? Because if it's even, I would take my previous result and I would just square it. If it's odd, like this three to the first equal three, I just multiply it by a, okay? So to check if it's odd, a fast way to do it is just to take b and by one, okay? Okay, so if this is odd, I'm going to take my result is going to equal to result, multiply by a, okay? Because that's just taking the result and multiplying it once by a, okay? All right, otherwise I'm going to square it. So to square it, that's going to equal to, so to square it, a is just going to equal to a times a. So yeah, that's basically the gist of this. And in the end, we just return result. This is basically the code, the iterative way to solve this problem because if you do this iteratively, this is going to basically, what this is going to do, it's the fastest way to do this log of n time. Log base two of n because that's the number of times you do it, okay? Yeah, so yeah, that's pretty much how binpower works. Now let's say I want to take the previous answer, a raised to b power, but I want to mod by a large number of n, large by m, mod by m, okay? So if you want to do this mod by m, I have the code down here. So this is to raise a to the b power and I'm taking the answer mod by m. Sometimes you have to mod by a large number to prevent overflow and that's what you would do. It's basically the same thing as the previous code. The only difference is that you are going to mod by m each time. So in this scenario, when you multiply by a itself, you just mod by m, okay? And in this scenario, when you square a times a, you mod by m. And yeah, in the beginning, you should also mod by m because that would reduce your a down. And the reason why this works is because if you take two numbers, right? If you do a mod by m, multiply a mod by m, those two numbers are going to be modded together. It's going to be the same number as a times a squared mod by m, right? If you mod them individually, it's going to equal to the same number. So that's the reason why bin power for the a to the raise to the b power mod by m works in this case. So yeah, I could also show you the recursive way to do it. I don't actually want to do that because it's actually way slower, but if you go here, I could just show you right here. So this is a recursive way to do it. What they're doing is that if they're dividing b by two every time, like what we do, but what they're doing is that you're going to recursively call the result dividing by two every time. And then they return the answer, multiply by itself. They calculate the answer. So here what they're doing is that if b is equal to zero, they return one. So that's kind of like what the same thing as the regular approach of doing it, the iterative approach, right? Because we're dividing by two every time, and then we pretty much multiply by itself. So this is kind of checking it out. Okay, then they take their result by recursively calling b over two that many number of times. That's what the result is. So here they're checking if mod by two is not equal to zero. This is what that's what this is doing. So this checks if it's odd. So if it's odd, we just multiply res times, res times a by itself once. And we're not actually, we're not calculating. We just multiply it a once. Otherwise we multiply res times res itself. So this is the recursive approach to do it. So yeah, that's pretty much the gist of how to do binary exponentiation. It's not too difficult, I guess, but it's not too easy, I guess. But yeah, rate, com, subscribe. I'll check you guys later. Peace.