Prove that $2^n\equiv2\bmod n$ whenever $n$ is prime $\gt2$

I came across the following observation while solving this imo problem $$2^n\equiv2\bmod n$$ whenever $$n$$ is prime $$\gt 2$$.

After running a python script for numbers till $$10^3$$ this gave the following numbers for which the above is true: $$[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 341, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 561, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 645, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]$$

Interestingly enough all of them turn out to be prime. However, I don't know how to proceed to formally prove the above statement. Any insight will be useful in moving toward the solution.

• Hint: See what lil' Fermat asserts. Commented Aug 1, 2021 at 16:34
• $341$, $561$, and $645$ are pseudoprimes base $2$, not primes; cf. this question Commented Aug 1, 2021 at 16:35
• $p\textrm{ prime}\Rightarrow a^p\equiv a\pmod p$ is true for all integers $a$ (cf. Fermat's little theorem). The other direction $a^p\equiv a\pmod p\Rightarrow p\textrm{ prime}$ is false in general, cf. Fermat pseudoprimes (as already mentioned by J.W. Tanner) Commented Aug 1, 2021 at 16:48
• But because this is for a contest problem, wouldn't the participants have to prove this special case of Fermat? Commented Aug 1, 2021 at 17:02
• This isn't a special case of Fermat. It is just a rewriting, generally taught as an observation. Commented Aug 1, 2021 at 17:18

An alternative hint: $$2^n = (1+1)^n$$.

Fermat's Little Theorem says that $$a^p \equiv a \pmod p.$$ for a prime number $$p$$ and an integer $$a$$.

Your hypothesis is an application of this theorem with $$a=2$$.

I recommend trying to prove the Fermat's Little Theorem on your own first, but here's my proof to it if you're stuck:

Indirectly, the statement means that any $$n^p - n$$ is divisible by $$p$$ for a certain prime $$p$$. Fix $$p$$. Let's take some examples first:

$$n^2 - n = (n)(n-1)$$. Since one of two consecutive numbers must be even (a multiple of $$2$$), the whole product is too.

$$n^3 - n = (n)(n^2 - 1) = (n)(n+1)(n-1)$$. Using the same logic from the first example, this works too!

Whenever we factor this kind of expression, we get $$(n)(n^{p-1} - 1)$$. Finding a general expression gives us an idea of using induction.

Base Case: $$n=1$$: Plugging $$n=1$$ into the original statement, we get $$1^p \equiv 1 \pmod{p}$$, which is obviously true since $$1^p = 1$$ no matter what $$p$$ is.

Inductive step: $$(n+1)^p \equiv n+1 \pmod{p}$$: We have to show that if the relationship is true for $$n$$, it must be true for $$n+1$$. Plugging in $$n+1$$ into the original statement instead of $$n$$, we get $$(n+1)^p \equiv n \pmod{p}$$. Using the binomial theorem, we get that $$(n+1)^p = n^p + {p \choose 1} n^{p-1} + {p \choose 2} n^{p-2} + \cdots + {p \choose p-1} n + 1.$$

Since $${p \choose q} = \frac{p!}{q! (p-q)!}$$, $$p$$ divides all $$p \choose q$$ for $$1 \le q \le p-1$$. Using this fact and taking $$\pmod{p}$$ on $$(n+1)^p$$, we get $$(n+1)^p \equiv n^p + 1 \pmod{p}$$. Since we know that $$n^p \equiv n \pmod{p}$$, we conclude that $$(n+1)^p \equiv n+1 \pmod{p}$$, as desired in the inductive step.

Our induction is complete. $$\square$$