Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Fast modular exponentiation, or, how to compute residues of numbers bigger than the number of atoms in the universe Suppose you’re given a large power of some number, like 5321 , and some other number, like 123. Is it possible to quickly find the remainder r of 5321 when it is divided by 123? Note that if you ask Wolfram Alpha, it says the number of atoms in the universe is thought to be about 1080 . So how can we possibly expect to compute 5321 and divide it by 123? The trick is that we don’t have to compute the full decimal expansion of 5321 in order to find r. Instead, we can exploit the arithmetic of exponents, and the fact that every remainder of any number divided by 123 is between 0 and 123. Doing so will keep us from ever having to compute a number bigger than 1232 . In detail, here is how this works. First, we write 321 as a sum of powers of 2. For convenience, I will start by writing down all powers of 2 less than 321: 20 = 1 21 = 2 22 = 4 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 28 = 256 29 = 512 We can write 321 = 256 + 64 + 1. (Basically, we are finding the binary expansion of 321. Indeed, if we rewrite the above equation as 321 = 1 · 256 + 0 · 128 + 1 · 64 + 0 · 32 + 0 · 16 + 0 · 8 + 0 · 4 + 0 · 2 + 1 · 1 = 1 · 28 + 0 · 27 + 1 · 26 + 0 · 25 + 0 · 24 + 0 · 23 + 0 · 22 + 0 · 21 + 1 · 20 then we see that we can write 321 as 101000001 in binary.) Now we observe that 5321 ≡ 5256 · 564 · 51 mod 123. Thus, if we can compute 5256 mod 123, 564 mod 123, and 51 mod 123, we can find 5321 mod 123. Now obviously 0 52 ≡ 51 ≡ 5 1 mod 123, and 1 52 ≡ 52 ≡ 25 mod 123. But instead of writing 2 52 ≡ 54 ≡ 625 mod 123, and leaving it at that, we can use long division to reduce, and write 2 52 ≡ 10 mod 123. 3 This pays off when we move to computing the remainder of 52 when we divide it by 123, since we can write 3 2 52 = 52 +22 2 2 = 52 52 , and, hence, 3 2 2 52 ≡ 52 52 ≡ 10 · 10 ≡ 100 mod 123. Hopefully you’ll agree with me that finding 10 · 10 mod 123 is a lot easier to do 3 than finding 52 ≡ 58 ≡ 390625 mod 123. 8 Now we do this trick repeatedly until we have found 52 mod 123. For example, we compute 4 3 3 52 ≡ 52 52 ≡ 1002 ≡ 10000 mod 123. Now, using long division, we see that 10000 = 81 · 123 + 37, so 4 52 ≡ 37 mod 123. 2 Continuing in this way, 37 = 1369, and long division shows 1369 = 11 · 123 + 16 so 5 52 ≡ 1369 ≡ 16 mod 123. And so on (I am just going to write the answers, but let me reiterate that all of this can be done easily by hand using long division): 6 52 ≡ 10 mod 123, 7 52 ≡ 100 mod 123, 8 52 ≡ 37 mod 123. Now we put together what we need to, and use long division one more time, to conclude 8 6 0 5321 ≡ 5256 · 564 · 51 ≡ 52 · 52 · 52 ≡ 37 · 10 · 5 ≡ 1850 ≡ 5 2 mod 123.