I have to compute $$2^{p-1} \mod p$$ and show by Fermat's little theorem that $p$ isn't prime. I know what the question is asking but I'm not sure how to reduce the exponent on $2^{p-1}$ to a more reasonable number. Does that make sense? Any general tips would be appreciated.
-
1Exponentiation by repeated squaring, reduce modulo $323$ at each step. – Daniel Fischer Nov 05 '13 at 22:05
-
Ok, thanks. I'm still not quite sure but thanks for getting me in the right direction – user1874239 Nov 05 '13 at 22:14
-
Do you know the algorithm? – Daniel Fischer Nov 05 '13 at 22:14
-
More explicitly, $2^2=4,4^2=2^4=16,16^2=2^8=256,\dots,(2^n)^2=2^{2n}$. Applying a modulus like $323$ ensures that the numbers stay within reach of a handheld calculator or even pen-and-paper. – abiessu Nov 05 '13 at 22:19
-
What I'm missing is why one would do anything fancy to test the primality of 323, which is obviously less than $20^2$ and turns out to be $17\cdot 19$ by quick trial and error. – dfeuer Nov 05 '13 at 22:19
-
2@dfeuer: or even observation that it is $18^2-1$... – abiessu Nov 05 '13 at 22:19
-
"I" get 157 as an answer, where by "I" I mean "Mathematica." – Cheerful Parsnip Nov 05 '13 at 22:21
-
1@dfeuer: I assume it's just as an exercise in Fermat's little theorem. – Nate Eldredge Nov 05 '13 at 22:30
-
1@GrumpyParsnip: I get the same answer below. – robjohn Nov 05 '13 at 22:39
-
@DanielFischer idea is the right one. Otherwise, $\large 2^{322}$ will cause an "overflow" in a 'current' computer. – Felix Marin Nov 06 '13 at 02:35
-
$\large 2^{322}\ {\rm mod}\ 323 = \color{#0000ff}{\Large 157}$. – Felix Marin Nov 06 '13 at 04:58
2 Answers
Exponentiation by squaring is the way to go.
First write the exponent in base two: $322=101000010_{\text{two}}$
Compute $2^n\pmod{323}$: $$ \begin{array}{} 2^n&n&\text{operation (mod $323$)}\\ \hline 1&0_{\text{two}}&\text{initialize}\\ 2&1_{\text{two}}&\text{square and multiply by $2$}\\ 4&10_{\text{two}}&\text{square}\\ 32&101_{\text{two}}&\text{square and multiply by $2$}\\ 55&1010_{\text{two}}&\text{square}\\ 118&10100_{\text{two}}&\text{square}\\ 35&101000_{\text{two}}&\text{square}\\ 256&1010000_{\text{two}}&\text{square}\\ 257&10100001_{\text{two}}&\text{square and multiply by $2$}\\ 157&101000010_{\text{two}}&\text{square}\\ \end{array} $$
Explanation of the Method:
When we square $a^n$, we double the exponent: $$ \left(a^n\right)^2=a^{2n} $$ When we square $a^n$ and multiply by $a$, we double the exponent and add one: $$ \left(a^n\right)^2\times a=a^{2n+1} $$ Write the exponent in base two.
Squaring our number doubles the exponent (shifting it left by one bit). Multiplying the number by the base adds one to the exponent.
The goal is to get the desired exponent ($322=101000010_{\text{two}}$), building it up from the left, by shifting left and optionally adding one, when needed. Every time we shift the exponent, we square our number. Every time we add one to the exponent, we multiply our number by the base.

- 345,667
-
Yeah, I don't really know what you're doing there. I get the idea but I cannot follow that. – user1874239 Nov 05 '13 at 22:56
-
@user1874239: I've added some explanation. Let me know if there are still questions. – robjohn Nov 05 '13 at 23:24
-
$\large\color{#888888}{\mbox{Following}}\ {\tt\mbox{@Daniel Fischer}}\ \color{#888888}{\mbox{comment}}$:
/* mod_0.cc http://math.stackexchange.com/questions/553472/how-to-compute-2-textsome-huge-power/553810#553810 */ #include <iostream> using namespace std; typedef unsigned long long ullong; const ullong ONE=ullong(1),N323=ullong(323),N322=ullong(322); int main() { ullong expo=ONE; for ( ullong n=0 ; n<N322 ; ++n ) { if ( (expo<<=ONE) >= N323 ) expo%=N323; } cout<<"REMAINDER = "<<expo<<endl; return 0; }
After running the program:
REMAINDER = 157
With $\tt WolframAlpha$: $$ 2^{322}\,{\rm mod}\,323 = \color{#ff0000}{\Large 157} $$

- 89,464