5

Here is the problem and the solution my text gives me.

enter image description here

When I first approached this problem, I looked up "repeated squaring" online and tried the following algorithm from khan academy.

Find the binary representation of the exponent

$383 \Rightarrow 101111111$

This means that the quantity $3^{383}$ can be written as

$3^{256+64+32+16+8+4+2+1}$

This also means that

$3^{383} = 3^{256}3^{64}3^{32}3^{16}3^8 3^4 3^2 3^1 $

Khan Academy's article says to take the mod of each one of those factors, multiply them together, and then reduce by the mod. While this makes sense, computing $3^{256}$ seems just as hard as $3^{383}$. Well that's fine, since we have a way to compute large exponents, we can recursively go through and compute $3^{255}$, etc. the same way, but this seems wasteful.

My text's solution seems much simpler to do by hand but I can't find an explanation of why or how it works.

Carpetfizz
  • 1,173

5 Answers5

6

For the binary method, you compute each of the relevant factors, like $3^{256},3^{64}$, etc., just by using repeated squaring. (Note that you can compute each of the factors you need by just saving them as you compute $3^{256}$.) Then you multiply them together (if they correspond to a $1$ in the binary expansion of the exponent of course). This adds up the exponents, so it all works out.

For the method in your text, you basically are going to step your way up to $3^{383}$ by repeatedly squaring what you have and then possibly multiplying by $3$ again, depending on whether the next exponent in the sequence, going right to left, is even or odd. In this case it turned out that you needed to multiply by $3$ again every single time except the first. This works basically because as you go through your sequence of exponents from right to left, the next number is either double the current number, or double it $+1$.

You can also view the method of your text recursively instead of iteratively. From this standpoint, to compute $3^{383}$ it is enough to compute $3^{191}$, square it and multiply by $3$; to compute $3^{191}$ it is enough to compute $3^{95}$, square it and multiply by $3$; etc.

Both ways are fine. The binary way is a little bit cleaner, because you don't have this case work to do at each iteration depending on whether the next exponent is even or odd, or the analogous case work in the recursive implementation. Accordingly, it would probably be faster on a computer for most inputs because of branch prediction. The way in your text requires a bit less memory (which could potentially matter if your modulus and/or the exponent were much larger). The binary way will require slightly fewer total operations.

Ian
  • 104,572
  • Thanks for the answer. The rest of the answers definitely helped me understand this better, but the part about considering even and odd cases is what helped me understand. I think I will go for the binary method in a written exam to make it less error prone. – Carpetfizz Mar 27 '17 at 01:33
3

You can get $3^{256}$ from $(3^{64})^4$ The approaches are similar. The difference is where you put in the odd factors. Your text's example computes $3^{23}=3^{11+11+1}=3^{11}3^{11}3$ whereas Khan would compute $3^{23}=3^{16+4+2+1}$ This is a particularly easy example for your text because above $5$ you always have $5 \times 5 \times 3 \equiv 5 \pmod 7$ but that is an accident. On average the Khan approach gives you a few less multiplies, but the difference is small. Use whichever you like.

Ross Millikan
  • 383,099
  • Thanks, but I'm struggling to see any similarities between the two methods. Do you mind expanding on "the difference is where you put in the odd factors" ? – Carpetfizz Mar 27 '17 at 01:22
  • 1
    If you want $3$ to some power of $2$, you just square it the appropriate number of times in either approach. The Khan approach has one term in the sum in the exponent for each one bit in the binary expansion of the exponent. $23=10111_2$ so there are four terms to multiply together after you have done the squaring. Your text's approach has a multiply by the base every time there is an odd exponent in the rising chain. This is one for every one bit in the base 2 expansion, so it works out just about the same. – Ross Millikan Mar 27 '17 at 01:37
3

Working through a step of your example will show why the book's method works. We have $$\lfloor 383 / 2 \rfloor = 191$$ ($\lfloor x \rfloor$ denotes the floor of $x$). Since $383$ is odd and thus rounding occurred, this implies that $$3^{383} \bmod 7 = 3 (3^{191})^2$$ Thus, if we knew the value of $3^{191} \bmod 7$, then it would be easy to find $3^{383} \bmod 7$ so the next step would be to continue recursively by computing $3^{191} \bmod 7$. Your text is following the same process except that it performs all the divisions and floorings first so that one can start with the smallest exponents first. This doesn't change any of the logic above but does make things more efficient.

Qudit
  • 3,261
  • Do you mind describing how this doesn't change the logic above? – Carpetfizz Mar 27 '17 at 01:18
  • @Carpetfizz It just starts with the last exponent instead of the first one and builds up from the bottom instead of working down from the top. – Qudit Mar 27 '17 at 01:24
  • Okay, I see that they start from 1 and go up to 383, but what's confusing me the most is why the exponents are completely different – Carpetfizz Mar 27 '17 at 01:25
  • @Carpetfizz My answer attempts to explain the books method independently from the Kahn Academy method, so my comment at the end refers to my answer and not the Kahn Academy approach. – Qudit Mar 27 '17 at 01:27
2

Both methods are related to the binary representation of the exponent. The ideas that tie all these things together is that every time you go one place to the left in a binary number, you multiply the number by two; and when you square a number, you multiply its exponent by two.

The method given by Khan Academy is what I would have thought of as "repeated squaring." From the binary representation, $\newcommand{two}{\mathrm{two}} 383_\mathrm{ten} = 101111111_\two,$ we determine that we need nine binary digits. The leftmost digit is the digit for $2^8 = 256.$ We therefore write our first power and then start squaring: \begin{align} 3^1 &\equiv 3 \pmod7 && \text{(first power)}\\ 3^2 &\equiv 2 \pmod7 && \text{(previous number squared)} \\ 3^4 &\equiv 4 \pmod7 && \text{(squared again)} \\ 3^8 &\equiv 2 \pmod7 && \text{(squared again)} \\ 3^{16} &\equiv 4 \pmod7 && \text{(notice the pattern?)} \\ 3^{32} &\equiv 2 \pmod7 \\ 3^{64} &\equiv 4 \pmod7 \\ 3^{128} &\equiv 2 \pmod7 \\ 3^{256} &\equiv 4 \pmod7 \end{align}

This becomes repetitive (and therefore easy) very quickly in this case: $2^2 \equiv 4 \pmod7$ and $4^2 \equiv 2 \pmod7,$ so it's just alternating $2$s and $4$s after the first few lines.

Next, we multiply together the powers that correspond to the binary digits we need: \begin{align} 100000000_\two &= 256 & 3^{256} &\equiv 4 \pmod7 \\ 101000000_\two &= 256 + 64 = 320 & 3^{320} \equiv 4 \times 3^{64} \equiv 4\times 4 &\equiv 2 \pmod7 \\ 101100000_\two &= 320 + 32 = 352 & 3^{352} \equiv 2 \times 3^{32} \equiv 2\times 2 &\equiv 4 \pmod7 \\ 101110000_\two &= 352 + 16 = 368 & 3^{368} \equiv 4 \times 3^{16} \equiv 4\times 4 &\equiv 2 \pmod7 \\ 101111000_\two &= 368 + 8 = 376 & 3^{376} \equiv 2 \times 3^8 \equiv 2\times 2 &\equiv 4 \pmod7 \\ 101111100_\two &= 376 + 4 = 380 & 3^{380} \equiv 4 \times 3^4 \equiv 4\times 4 &\equiv 2 \pmod7 \\ 101111110_\two &= 380 + 2 = 382 & 3^{382} \equiv 2 \times 3^2 \equiv 2\times 2 &\equiv 4 \pmod7 \\ 101111101_\two &= 382 + 1 = 383 & 3^{383} \equiv 4 \times 3^1 &\equiv 5 \pmod7 \end{align}

In practice, of course, we only need to write the rightmost equivalences on each line. The rest of it is just to show why it works. Basically, the idea is that you can get the exponent $101111101_\two = 383$ by taking the sum \begin{multline} 100000000_\two + 1000000_\two + 100000_\two + 10000_\two \\+ 1000_\two + 1000_\two + 100_\two + 10_\two + 1_\two. \end{multline} We construct the power of $3$ by multiplying the powers corresponding to the addends in this sum. Note that you could multiply powers starting with the highest power (as shown above) or starting with the lowest power.

But where the Khan Academy method simply adds the various powers of $2$ to get the exponent, the method given in your text corresponds to a "shift left and add" method of building the exponent. That is, we start with the leading bit of the binary exponent, but we put it in the one's place. We then repeatedly multiply by $2$ (which "shifts" all the binary digits one place to the left) and (when needed) add $1$ to write another digit of the number, working from left to right. Doubling the exponent corresponds to squaring the power of $3$ and adding $1$ corresponds to multiplying by $3$: \begin{align} 1_\two &= 1 & 3^1 &\equiv 3 \pmod7 \\ 10_\two &= 2 \times 1 = 2 & 3^2 &\equiv 2 \pmod7 \\ 101_\two &= 2 \times 2 + 1 = 5 & 3^5 \equiv (3^2)^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 1011_\two &= 2 \times 5 + 1 = 11 & 3^{11} \equiv (3^5)^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 10111_\two &= 2 \times 11 + 1 = 23 & 3^{23} \equiv (3^{11})^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 101111_\two &= 2 \times 23 + 1 = 47 & 3^{47} \equiv (3^{23})^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 1011111_\two &= 2 \times 47 + 1 = 95 & 3^{95} \equiv (3^{47})^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 10111111_\two &= 2 \times 95 + 1 = 191 & 3^{191} \equiv (3^{95})^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \\ 101111111_\two &= 2 \times 191 + 1 = 383 & 3^{383} \equiv (3^{191})^2 \times 3 \equiv 4 \times 3 &\equiv 5 \pmod7 \end{align}

As long as we do not make any arithmetic errors, we inevitably get the same result on the left as when we do it the other way, therefore we get the same result on the right as well.

David K
  • 108,155
0

Let's simplify that a bit more :

 3 ^ 383
 383 := [ 1 1111 1101 ] LSB bitstring

          1  1  1  1  1 
             1  1  0  1
     -------------------
     :=   3*(3*(3*(3*(3*(
             3*(3*(::(3*(1)^2)^2)^2)^2)
                           ^2)^2)^2)^2


            1  1  1  1  1  1  1  0  1
         --------------------------------
            3*(3*(3*(3*(3*(3*(3*(  (3
               )^2)^2)^2)^2)^2)^2)^2)^2

   3 ^ 383

= 3(3(3(3(3(3(3*( (3)^2)^2)^2)^2)^2)^2)^2)^2

technically you can even directly do it in base-10 (decimal) instead of splitting into a bits :

   = (3^3) * ((3^8) * ( 3^3 )^10)^10
   =  27   * ( 6561 *    27  ^10)^10
   = (3^3) * ((3^8) * (3 ^  30) )^10
   = (3^3) * (         3 ^  38  )^10
   = (3^3) * (         3 ^ 380  )
   =  3 ^ 383

or any base you like, say, base-7 (LSB := 5501)

   = (3^5) * ( (3^5) * ( (3^1)^7)^7)^7

= 243 * ( 243 * (2187)^7 )^7

= (3^5) * (3^ 54)^7 = 3^5 * 3^378


= 3^383


   3 ^ 383 % 7

= 3 * (3 * (3 * (3 * (3 * (3 * (3 * ( (3 )^2 % 7)^2 % 7)^2 % 7)^2 % 7)^2 % 7)^2 % 7)^2 % 7)^2 % 7

= 3(3(3(3(3(3(3(3^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7 = 3(3(3(3(3(3(3( 2 )^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7 = 3(3(3(3(3(3( 5 )^2 %7)^2 %7)^2 %7)^2 %7)^2 %7)^2 %7 = 3(3(3(3(3*( 5 )^2 %7)^2 %7)^2 %7)^2 %7)^2 %7 = 5

another shortcut way is

    3^3 ≡ -1 (mod 7) -----
 := 3^6 ≡  1 (mod 7)      |
                          |
 := 3^( 383 %  6 ) % 7    |
 :=        3^( 5 ) % 7    |
 := (-1) * 3^( 2 ) % 7 <——
 :=         -9     % 7 
 ----------------------
           ≡ -2 (mod 7)
           ≡  5 (mod 7)