 In this video, we are going to summarize the entire content for section 4.3 entitled the method of repeated squares from Tom Judson's abstract algebra textbook. Now, in chapter 4 about cyclic groups, as the name suggests, we've been studying cyclic groups, cyclic subgroups, the order of elements, because the order of an element is going to be the order of the corresponding cyclic subgroup generated by that element. And so the previous properties of cyclic groups that we've learned allow for simple calculations of orcalation of elements. So, like for example, if we did something like, oh, let's take the cyclic subgroup Z24 and I want to consider what is the order of the element say 9, right? Well, we have to first calculate the GCD between 9 and 24, and we're going to divide that from 24. Well, the GCD is 3, so we get 24 over 3, which is going to give us 8. So the order of this element 9 is 8. As GCDs can be calculated using Euclidean algorithm very quickly, this is essentially a simple calculation of what we could do. A general group, not a cyclic group necessarily, computing the order of an element can be a much more difficult process. Now, one algorithm to determine the order of a group in the absence of Lagrange's theorem, which we'll talk about, a naive approach to computing the order of an element is just to calculate G, is that the identity? Nope. G squared, is that the identity? Nope. G3, is that the identity? Nope. G4, is that the identity? Nope. And you just keep on going bigger, bigger, bigger, bigger, bigger, bigger, until you find the identity. Woo-hoo, right? Assuming the process even terminates, right? So if you're in a finite group, you can't expect that eventually this is going to equal the identity. But if it's a large group, right, if we have like a billion elements, that could take some time, right? If we have a trillion elements, if we're in like the monster group, this could take a long time. It's not a very effective algorithm because it depends on how big N is. Now, fortunately, with Lagrange's theorem, which is something that we'll talk about in the future, is that with Lagrange's theorem, we can dramatically shrink this process. But still, it's really time consuming and it depends on factoring the number N, which in general factoring a large number is a very hard process. Computer security is kind of based upon, at least like the RSA protocol, is based upon the difficulty of factoring big numbers being a hard problem. Now, despite that, you know, so let's assume, you know, let's assume that this is our algorithm. We just calculate big powers of these things. In order to do that, we still would have to actually compute something like how are you going to do something like that? You know, how do you do a really big large power? Like what if we have a group? What do we have a group that its order is 2 to the 1, 0, 0, 0, 0, 0 or whatever? You know, if you take 2 power, this number is going to be so monstrously large, we might not expect to be able to compute such a thing. So exponent calculations in general could be a hard problem. Now, we want to focus on a very special case right now in finite groups, right? We're not going to focus on addition, we're going to focus on multiplication, right? If we're working say with a modulus like 4,000, or 46,389, how do we compute a power like this? 2 to the 3, 7, 3, 9, 8, 3 commas, what do I have to put into that? 37 million? That would be a very difficult thing if we're just doing like 2 times 2 times 2 times 2 times 2 times 2. You can see that's going to take a long, long, long time. But it turns out that with modular arithmetic, we can speed up this process of the power calculation very, very quickly. And so we can actually reduce the complexity from n to something much, much smaller. I want to show you how to do that. So how do we compute this element? Maybe we're like, oh, the order of 2 is in this modulus, I don't know, what do we exponent? We can do this really, really quickly in the following way. So what we're going to first do is we're going to take our exponent in play, and we're going to write it as a binary expansion. So I want to write it as a combination of powers of 2. So you take like this power 37, 39, 8, 3, 3, 2, and I want you to write that as a binary expansion. So this is like 1 plus 2 plus 4 plus 8 plus 16, whatever those turn out to be, compute the binary expansion. We'll do a specific example of this really quickly. Then as you compute this binary expansion, you're going to get this like 2 to the k1, 2 to the k2, 2 to the kR all the way up. And so then you're going to start computing powers. Compute a to the 2 to the 0 power. That's just the identity. Then you're going to do, I'm sorry, that's just going to 2 to the 0 is just 1, so you're just going to get an a right here. Then the next one, you're going to do a squared. Then you're going to do a to the 4th. Then you're going to do a to the 8th. And then a to the 16th all the way up to a to whatever you have 256, 2048, whatever, whatever power, subsequent powers of 2. Now, I want you to be aware that it's not too hard to do here. This is where the algorithm repeated squares comes into play. So if you're doing like a to the 1st, a squared, a to the 4th, a to the 8th, et cetera, you're going all the way up to the power. Notice that getting a squared just means a times a. Getting a to the 4th is just a squared squared. So you just square the previous number. And then the next step, a to the 8th, that's just going to be a to the 4th squared. So you just repeat, you just square the elements in a moment ago. And so this method of repeated squares is you just create this list of numbers and you're just going to square the previous number in this list. All of these numbers right here. Well, then by X one of rules, if A has this binary expansion, X to the A is going to be congruent to X to this binary expansion, which each of these numbers are in the list. You put them all together and voila, you can reduce mod n when you're done and that gives you the number you want. And so we have to be able to compute a binary expansion, which is not too hard of a thing to do. Then we compute all of these repeated squares and then we multiply the product of them at the end. And if you reduce the numbers along the way, you actually never are going to get a number that's larger than the modulus squared, which is a good thing. And basically worst case scenario, this calculation would actually only take two log two of A minus one many steps where A was the exponent in play here. So this is actually a logarithmic algorithm, which those don't show up in computer science all the time, right? They're pretty rare and we should value them with this explicit example of how one would do something like this. So we're going to keep things not too crazy because we want to do it by hand. Ironically, you don't see me doing my air quotes with my hands. We're going to do this thing without a computer, although by all means we might use like a four function calculator to help us with the arithmetic. Who does arithmetic by themselves anymore? We're going to compute the number 271 raised to the 321st power and we're going to reduce this mod 481 using the method of repeated squares. So the first thing to do is we need to find a binary expansion for 321. So it's the largest power of two that's smaller than 321. Well, again, if you're really good at the game 2048, you have all of the powers of two memorized up to two to the 11th. And if you're really good, you know a couple after that, right? The largest power of two here is going to be 256. If you take 321 minus 256, that's going to be 65, which 65 is 64 plus one, which are all powers of two. So therefore the binary expansion of 321 is 256 plus 64 plus one, 256 is two to the eighth, 64 is two to the sixth and one is two to the zero. So therefore we're going to have to compute up to we're going to have to do this process eight times. We're going to start off. By taking the repeated powers of two here. So we're going to start off with 271 to the two to the zero power, which is two to the first. That's of course, it's going to be 271. No reduction needed there whatsoever. Then we do the next number of the sequence, which is going to be two to the first. Two to the first means we take 271 and we square it. 271 times 271 will be 73441, which when you reduce that mod 481, you get 329. The next number, and so we're going to kind of measure these numbers in the sequence. We get 271, 329, these are the numbers we want. The next number in the sequence, we're going to take 271 raised to the two squared power, right? The power of two is just going to increment each time as we go through this process, right? So we're going to take 271 to the fourth power, but 271 is just 329 squared or 329 was the previous number. So we just we just square the previous number. That's going to give you 108,241, which reduces to 16 mod 481. So we get 16. Now the next number on the sequence, we're going to take 271 raised to the two cubed power, which is the eighth power. But taking 271 to the eighth power is just squaring the previous number. You get 16 squared, which is just 256 and then no reductions necessary. And so to kind of show you how quick this procedure is, I want you to see what happens next. We're going to take 256 and we're going to square it. That gives you 65,536. It reduces to 120 when we do mod 41. We're then going to take 120 squared, which is equal to 140,400, which then reduces to be 451 mod 481. Then we're going to take 451. We square it. That becomes 203,401. We reduce that mod 481 and you get 419. The next step is you have to then square 419, which becomes 1175,561. And then that reduces to 477 mod 41. Then you're going to square 477. Then that becomes 227,529, which reduces to 16, which you notice 16 was actually on this list. This list came repeat itself over and again. And this is actually great if we did because then we can just copy numbers if we had to keep on going. But this is now the eighth step, right? Because you have to start at zero, right? So notice this would be if you just kind of keep track of this is my zero step. One, two, three, four, five, six, seven, eight. So all of this business over here, we can kind of ignore really. We just have to kind of keep track of we could write a for loop for a computer program or a while loop or whatever you want to do. We could just do a for loop and be like, okay, starting at zero, we got to do this. We got to get to stage, stage eight, and we just do subsequent squares. We square reduce, square reduce, square reduce, square reduce, square reduce, square reduce, square reduce. That's what the repeated algorithm, the repeated squares algorithms all about. Just repeat the squares until you've done it sufficiently many times where we got eight from the binary expansion and then zooming back into this thing. So now that we have all these numbers, how do you do 271 to the 321 power? Well, 321 remember was 256 plus 64 plus one. So we have to take 271 to the first power check. Then we have to take 271 to the 64th power. That's going to be two to the sixth. That's the sixth step right here. That's 419 check. Then we have to do 271 to the two to the eighth power. Eighth was the eighth step here was 16 check. So then we do 271 times 48419, which gives you 113,000 times for 549 reduce that that becomes a 33. Then you're going to take 33 times 16, which is 528, which reduces down just to be 47. And so voila, we did the, we did the repeated squares algorithm, which we only figure out that 271 to the 321 power mod 48 is just 47. And we did it with only basically, we had the eight squares we had, which each of those it has a cost of a multiplication and a reduction. And then we had two multiplications with reductions. So basically we had to do 10, 10 multiplications and 10 reductions, plus the binary expansion we did earlier. So in terms of calculations, we're looking at, you know, really only looking about like less than 15 actual calculations. You know, we'll say just 20, you know, kind of those reductions and such. Again, these are all efficient steps and we can done really, really quickly. Now I kind of point out to you that these type of calculations, although they can be done by hand, they really ought to be done with the computer algebra. For my students viewing this video, of course, you do it have to do this sort of by hand. And again, feel free to use like a scientific calculator to help you. But I do want to kind of see steps by steps by steps. But in general, if someone was doing a calculation like this, you would do it on a computer. And so actually does offer magma as a computer algebras soft as a computer algebras system that amongst other things actually can do the repeated squares algorithm. So you use the command mod exp. So this is for doing modular exponents. If you take the numbers 271, 321 and 481, the way you're supposed to read this, if you want to do X to the a mod n, that you would use the command capital mod exp. You would then do x, a, n, and then you always have to end with a semicolon so it compiles correctly. If you run that procedure on magma, you'll get 47 right here. Now, one thing I want to mention about magma is you can actually access it free online using the following the following URL, which you can actually find that in the link of this video right here. But we can type in the command like we said a moment ago, mod exp, and our number was 271 raised to the 321 power, and we're going to work mod 481. Let's run that. You can see the output below 47. It was almost instantaneously. Magma computed this in .01 seconds. So 100th of a second it took to do this on the computer right here. And this is the free version of magma, right? You can access this on the web for free. There are some limitations to this. Like for example, any calculation that requires more than two minutes to terminate, then this thing will stop you automatically. Of course, the purchase version, the license version is a little bit better than that, but this will be sufficient for many of your calculations, right? So we can actually use magma to do that. Now, I want you to be aware that we can also show some of the intermediate steps. So for example, if we wanted to calculate a binary expansion, we showed how to do it, take your exponent A and you want to do the binary expansion, use the command int sync. So this is going to switch your integer to a sequence here. It'll be a binary sequence. So if you run that, it'll output this array right here. Which the way you want to do this, this is the zero spot 1, 2, 3, 4, 5, 6, 7, 8. So 321 is 1 times 2 to the 0, 0 times 2 to the first, plus 0 times 2 squared, plus 0 times 2 cubed, plus 0 times 2 to the fourth, plus 0 times 2 to the fifth, plus 1 times 2 to the sixth here, plus 2 times, or 0 times 2 to the seventh, plus 1 times 2 to the eighth. That's what this sequence is doing here. It just gives us the coefficients in this binary expansion. Ones mean that you include that power of two, where you start zero going up to the eighth power right there, since there are nine elements in this array. So that gives us the binary expansion. That should say 321 right there. Sorry about all the typos we sometimes see here. And so then we could then program, if calculated, 329 squared mod 48, then we take the next time that was the 16, and then we do the next number, 16 squared mod 48, that would give you, you know, 256. Just keep on going, keep on going, you can run a for loop, and then you can compute the thing at the very end. So I am happy with you using calculators to help you. But the point is, if you're going to use a calculator, you have to know what the calculator is doing. You can't just plug in the formula that does everything. You have to earn the right. And so, at this point in the semester, you know, if you follow along with the series, you've done things like reduction of integers, mod, whatever. So by all means use a scientific calculator or you can use MAG if you don't have one to do the reductions to save you some time. I don't really care about arithmetic, like can I do, can I do 471 times 419. If, if you're watching this video, I assume you've proven you can do arithmetic. And therefore, feel free to use a scientific calculator. If you're watching this video, it's a good chance you have not yet earned the right to do the repeated squares algorithm on magma here. So therefore, what you have to do is kind of do it by hand to earn the right or write a for loop yourself using magma or C++ Java, whatever program you want. If you could write a program that would convince me that you understand how to do it as long as you're able to write it yourself. And so it's what's really nice about this repeated, this repeated squares algorithm is that it's a very, very fast algorithm caution you that if you did something in magma like the following, if you did like 271 raised to the carat is for exponents raised to the 321 power, and then you did mod of 481. That would take a long time to compile. So like if we went back to magma right here, and we tried something like that instead of this one. If we tried to do, let's actually type it in here, if we did 271 raised to the 321 power mod 481, and you hit enter, like that, it's going to take a whole lot longer to do. Now admittedly, it took twice the amount of time so it took 0.02 of a second right, and that doesn't seem so horrible right because that's because with this numbers right here magma what it did is it actually calculated 271 raised to the 321 power mod 481 and you hit enter like that. And that doesn't seem so horrible right because that's because with this numbers right here magma what it did is it actually calculated 271 to the 321 power and then reduce it then do any reduction along the way. So that wasn't so bad. Now if we did something like mod exp, let's pick some bigger numbers we'll take 271. But this time let's do something quite larger. Let's take 12345678, 481. If we try that one. We got 38 same amount of time right our X1 was a lot bigger right but not a big deal, but on the other hand if we tried to do 271 raised to the 234567678 power, and we want to reduce this mod 481, you're going to start to see there's going to be a big difference in quality. Notice the computer's thinking thinking thinking. It took about two seconds to do that one as opposed to the point zero two seconds. So I mean that's looking like 100 times worse in terms of quality. And again, I can exacerbate this problem just by adding more digits. You can see that it just basically it's too big right 271 to attend to number it's like magma just gives up I'm not going to compute a number that big. But if we get rid of this line, and we do 90 you run the repeated squares algorithm, we end up with 38 still, and it still only took about one 100th of a second. The difficulty with his larger exponent didn't make much of a difference whatsoever. And so this kind of shows you the impressiveness of that algorithm right there. We can use smart mathematics to use that as good algorithms to help us out here. And these things are learning about groups, cyclic groups, and other types of things actually have real consequences not just for mathematicians, but as real consequences in other fields like computer science will explore some more of these in this in this course, that there are applications really cool applications of group theory applied to computer science. And it does depend on fast algorithm.