1

How do I simplify the following:

$a^{b} \ \pmod {b}$ where $b$ is very large.

For example:

$2^{499} \pmod {999}$

How do I find the result without computing all?

I don't want to use such a quantity of memory for storing the value of $2^{499}$.

Is there any sort of property to find the result in a faster way?

Crystal
  • 310
  • 1
    If $b>2^{499}$ then the answer is $2^{499}$. No shorter way to write it. If $b<2^{499}$ then you can iterate squaring $\pmod b$ so at least you keep the number you need to store $<b$. – lulu Oct 17 '19 at 21:34
  • In many programming languages there is a built in function to do this. For example python has pow(2, 499, 999). – Peter Foreman Oct 17 '19 at 21:35
  • @lulu what do you mean by squaring (mod b) ? if b is not squarable? – Andri Nic Oct 17 '19 at 21:37
  • iterated squaring: compute $2\mapsto 2^2\mapsto 2^4\mapsto 2^8\mapsto 2^{16}\cdots$ all $\pmod {b}$. by squaring each term, and reducing $\pmod b$. – lulu Oct 17 '19 at 21:40
  • @AndriNic You're welcome. Although the other question asks about doing it "by hand", much of what the answers provide can also be fairly easily implemented on a computer. Also, the answer gives a link & explains how to use the iterate squaring method that lulu's comment mentioned originally, and expanded on in the comment just above. For general cases, i.e., where you don't have any particular knowledge or limitations you can use, I believe it's possibly the relatively simplest & most efficient method to use on a computer. – John Omielan Oct 17 '19 at 21:42
  • That is not an equation! – Allawonder Oct 17 '19 at 22:22

1 Answers1

1

Note that $999=27\times 37$

Now Fermat-Euler Theorem tells us that $2^{18}\equiv 1 \bmod 27$ and $2^{36}\equiv 1 \bmod 37$ whence $2^{36}\equiv 1 \bmod 999$

So, for a start, we can reduce $499$ modulo $36$ before doing any of the detailed calculations.

Or notice that $2^{10}=1024\equiv 25 = 5^2\bmod 999$ and use Fermat-Euler with $5$ instead of $2$.

Or combine both approaches and notice some other things too.

Mark Bennet
  • 100,194