JB Math Club 26-27 · Problem set

Wilson’s, Fermat’s, and Euler’s Theorems

Modular arithmetic · Problem set

Problem 1. A factorial modulo a prime

What is the remainder when \(70!\) is divided by \(71\)?

Show solution

Solution. Apply Wilson's theorem

Since \(71\) is prime, Wilson's theorem gives

\[70!=(71-1)!\equiv-1\pmod{71}.\]

The nonnegative representative of \(-1\) modulo \(71\) is \(70\).

\[\boxed{70}\]

Problem 2. Restore the missing factors

What is the remainder when \(80!\) is divided by \(83\)?

Show solution

Solution. Compare with Wilson's factorial

The number \(83\) is prime. Wilson's theorem applies to \(82!\), which differs from \(80!\) by two factors:

\[82!=80!\cdot81\cdot82\equiv80!\cdot(-2)(-1)=2\cdot80!\pmod{83}.\]

Consequently, \(2\cdot80!\equiv-1\equiv82\pmod{83}\). Since \(2\) is coprime to \(83\), this congruence has a unique solution for \(80!\) modulo \(83\). The number \(41\) works because \(2\cdot41=82\).

\[\boxed{41}\]

Problem 3. A factorial modulo a prime square

What is the remainder when \(201!\) is divided by \(10201\)?

Show solution

Solution. Separate the only multiple of 101

First observe that \(10201=101^2\). Among \(1,2,\ldots,201\), the only multiple of \(101\) is \(101\) itself. Write

\[201!=101A,\qquad A=100!\prod_{r=1}^{100}(101+r).\]

We only need \(A\) modulo \(101\): multiplying a congruence modulo \(101\) by \(101\) gives a congruence modulo \(101^2\). Reducing the factors of the second block gives

\[A\equiv100!\prod_{r=1}^{100}r=(100!)^2\equiv(-1)^2=1\pmod{101},\]

where the last congruence is Wilson's theorem for the prime \(101\). Thus \(A=1+101t\) for some integer \(t\), and

\[201!=101+10201t.\]

\[\boxed{101}\]

Wilson's theorem was used modulo the prime \(101\). It does not apply directly to the composite modulus \(10201\).

Problem 4. Reduce the base and the exponent

What is the remainder when \(200^{10001}\) is divided by \(101\)?

Show solution

Solution. Use a block of 100 powers

We have \(200\equiv-2\pmod{101}\). Since \(101\) is prime and does not divide \(2\), Fermat's little theorem gives \((-2)^{100}\equiv1\pmod{101}\). Now \(10001=100\cdot100+1\), so

\[200^{10001}\equiv(-2)^{10001}=\bigl((-2)^{100}\bigr)^{100}(-2)\equiv-2\equiv99\pmod{101}.\]

\[\boxed{99}\]

The base is reduced modulo \(101\); the exponent is reduced modulo \(100\).

Problem 5. Notice the square in the base

What is the remainder when \(9^{1010}\) is divided by \(2017\)?

Show solution

Solution. Rewrite the power using base 3

The number \(2017\) is prime, and \(3\) is coprime to it. Fermat's little theorem gives \(3^{2016}\equiv1\pmod{2017}\). Rewriting \(9=3^2\) makes the useful exponent visible:

\[9^{1010}=3^{2020}=3^{2016}\cdot3^4\equiv3^4=81\pmod{2017}.\]

\[\boxed{81}\]

Applying Fermat directly to base \(9\) would give exponent \(2016\), which is larger than \(1010\). Changing the base avoids a long calculation.

Problem 6. One power short of Fermat

What is the remainder when \(3^{125}\) is divided by \(127\)?

Show solution

Solution. Find a modular inverse

Since \(127\) is prime and does not divide \(3\), Fermat's little theorem gives \(3^{126}\equiv1\pmod{127}\). Thus

\[3\cdot3^{125}\equiv1\pmod{127}.\]

We need the inverse of \(3\). It is \(85\), because

\[3\cdot85=255=2\cdot127+1.\]

The inverse is unique modulo \(127\), so \(3^{125}\equiv85\pmod{127}\).

\[\boxed{85}\]

Problem 7. Three totients

Calculate \(\varphi(1)+\varphi(101)+\varphi(450)\).

Show solution

Solution. Use the prime factorization

By definition, \(\varphi(1)=1\): the integer \(1\) is coprime to \(1\). Since \(101\) is prime, \(\varphi(101)=100\).

Next, \(450=2\cdot3^2\cdot5^2\), so the totient formula gives

\[\varphi(450)=450\left(1-\frac12\right)\left(1-\frac13\right)\left(1-\frac15\right)=120.\]

Each distinct prime contributes one factor, regardless of its exponent in the factorization.

\[\boxed{\varphi(1)+\varphi(101)+\varphi(450)=1+100+120=221}\]

Problem 8. A power modulo a composite number

What is the remainder when \(7^{963}\) is divided by \(450\)?

Show solution

Solution. Reuse the totient

We have \(\gcd(7,450)=1\), and Problem 7 gives \(\varphi(450)=120\). Euler's theorem therefore gives \(7^{120}\equiv1\pmod{450}\).

Since \(963=8\cdot120+3\),

\[7^{963}=(7^{120})^8\cdot7^3\equiv7^3=343\pmod{450}.\]

As \(0\le343<450\), this is already the required remainder.

\[\boxed{343}\]

Problem 9. Remarkable numbers

A positive integer \(n\) is called remarkable if \(2^{n!}-1\) is divisible by \(n\). How many remarkable numbers not exceeding \(1000\) exist?

Show solutions · 2 methods

Solution 1. Characterize all remarkable numbers

No even integer works. The number \(2^{n!}-1\) is odd, so it cannot be divisible by an even \(n\).

Every odd integer works. First, \(n=1\) works because every integer is divisible by \(1\). Now let \(n>1\) be odd. Then \(\gcd(2,n)=1\), so Euler's theorem gives

\[2^{\varphi(n)}\equiv1\pmod n.\]

The positive integer \(\varphi(n)\) is at most \(n-1\), so it occurs among the factors of \(n!\). In particular, \(\varphi(n)\mid n!\). Write \(n!=t\varphi(n)\). Then

\[2^{n!}=\bigl(2^{\varphi(n)}\bigr)^t\equiv1\pmod n.\]

Thus the remarkable integers are exactly the odd positive integers. There are \(500\) of these in \(1,2,\ldots,1000\).

\[\boxed{500}\]

Solution 2. Use repeated remainders

Here is another proof that every odd \(n>1\) works, using only the pigeonhole principle and cancellation. Consider the \(n+1\) numbers

\[2^0,2^1,2^2,\ldots,2^n.\]

There are only \(n\) possible remainders modulo \(n\), so \(2^i\equiv2^j\pmod n\) for some \(0\le i<j\le n\). Since \(n\) is odd, \(2^i\) is coprime to \(n\), and we may cancel it:

\[2^{j-i}\equiv1\pmod n.\]

Put \(d=j-i\). Since \(1\le d\le n\), we have \(d\mid n!\). Raising the last congruence to the integer power \(n!/d\) gives \(2^{n!}\equiv1\pmod n\). Together with the even case and \(n=1\), this again gives exactly \(500\) remarkable integers.

Problem 10. The last three digits

What three-digit number is formed by the last three digits of \(7^{999999}\)?

Show solutions · 2 methods

Solution 1. Turn a huge power into an inverse

The last three digits are determined by the remainder modulo \(1000\). Since \(\gcd(7,1000)=1\) and

\[\varphi(1000)=1000\left(1-\frac12\right)\left(1-\frac15\right)=400,\]

Euler's theorem gives \(7^{400}\equiv1\pmod{1000}\). The exponent \(1000000=2500\cdot400\) is especially convenient:

\[7\cdot7^{999999}=7^{1000000}=(7^{400})^{2500}\equiv1\pmod{1000}.\]

Thus the desired remainder is the inverse of \(7\) modulo \(1000\). Since \(7\cdot143=1001\equiv1\pmod{1000}\), we obtain

\[\boxed{143}\]

Solution 2. Work modulo 8 and modulo 125

Modulo \(8\), the base is \(7\equiv-1\), and the exponent is odd. Therefore

\[7^{999999}\equiv7\pmod8.\]

Modulo \(125\), we have \(\varphi(125)=100\) and \(\gcd(7,125)=1\). Euler gives \(7^{100}\equiv1\pmod{125}\). Since \(100\mid1000000\), the desired power is the inverse of \(7\) modulo \(125\). That inverse is \(18\), because \(7\cdot18=126\).

The number \(143\) satisfies both required congruences:

\[143\equiv7\pmod8,\qquad143\equiv18\pmod{125}.\]

Hence \(7^{999999}-143\) is divisible by both \(8\) and \(125\). These numbers are coprime, so their product \(1000\) also divides the difference. Since \(143\) lies between \(0\) and \(999\), the last three digits are again \(143\).

Problem 11. An exponent that is itself a power

Given a positive integer \(m>1000\), what is the remainder of \(2^{3^m}\) when divided by \(3^{m+1}\)? Express the answer in terms of \(m\).

Show solutions · 2 methods

Solution 1. Gain one factor of 3 at each step

We will prove the following stronger statement by induction:

\[3^{k+1}\mid2^{3^k}+1\qquad\text{for every integer }k\ge0.\]

For \(k=0\), this is \(3\mid2+1\), which is true. Suppose it holds for some \(k\ge0\), and put \(A=2^{3^k}\). Then \(3^{k+1}\mid A+1\), so in particular \(A\equiv-1\pmod3\). Now factor:

\[2^{3^{k+1}}+1=A^3+1=(A+1)(A^2-A+1).\]

The first factor is divisible by \(3^{k+1}\), and the second is divisible by \(3\), because

\[A^2-A+1\equiv(-1)^2-(-1)+1=3\equiv0\pmod3.\]

The product is therefore divisible by \(3^{k+2}\), completing the induction. Taking \(k=m\) gives \(2^{3^m}\equiv-1\pmod{3^{m+1}}\). The required nonnegative remainder is

\[\boxed{3^{m+1}-1}\]

Solution 2. Use Euler and identify the sign

Put \(X=2^{3^m}\). Since \(2\) is coprime to \(3^{m+1}\) and \(\varphi(3^{m+1})=2\cdot3^m\), Euler's theorem gives

\[X^2=2^{2\cdot3^m}\equiv1\pmod{3^{m+1}}.\]

Thus \(3^{m+1}\mid(X-1)(X+1)\). The two factors differ by \(2\), so they cannot both be divisible by \(3\). Consequently, all \(m+1\) factors of \(3\) must come from one of them.

Because \(3^m\) is odd, we have \(X\equiv(-1)^{3^m}=-1\pmod3\). Thus \(X-1\) is not divisible by \(3\); the factor divisible by \(3^{m+1}\) must be \(X+1\). This again gives remainder \(3^{m+1}-1\).

The argument actually works for every integer \(m\ge0\). The large lower bound in the problem does not change the answer. In the second solution, Euler gives the square congruence; the argument about the two factors is needed to determine the sign.

Problem 12. Keep as many cards as possible

Given a prime number \(p>1000\), Alice has \(p-1\) cards, each labeled with a number from \(1\) to \(p-1\), with each label used once. She wants to take as many cards as possible such that the product of their labels leaves remainder \(123\) when divided by \(p\). How many cards should she take? Express the answer in terms of \(p\).

Show solution

Solution. Prove a bound, then choose the omitted card

She cannot take all the cards. By Wilson's theorem, their product is

\[(p-1)!\equiv-1\pmod p.\]

This is not congruent to \(123\): otherwise \(p\mid124\), impossible for \(p>1000\). Thus she can take at most \(p-2\) cards.

She can take exactly that many. Since \(p\) is prime and greater than \(123\), the number \(123\) has an inverse modulo \(p\). Choose the unique \(b\in\{1,2,\ldots,p-1\}\) satisfying

\[123b\equiv-1\pmod p\qquad\bigl(b\equiv-123^{-1}\pmod p\bigr).\]

Omit the card labeled \(b\), and let \(P\) be the product of the remaining labels. Then

\[bP=(p-1)!\equiv-1\equiv123b\pmod p.\]

Since \(b\) is coprime to \(p\), cancellation gives \(P\equiv123\pmod p\). This construction uses \(p-2\) cards and attains the upper bound.

\[\boxed{p-2}\]

Problem 13. Combine two prime moduli

Given distinct prime numbers \(p\) and \(q\), both greater than \(1000\), what is the remainder of \(p^q+q^p\) when divided by \(pq\)? Express the answer in terms of \(p\) and \(q\).

Show solutions · 2 methods

Solution 1. Exhibit a multiple of the whole modulus

Since \(p\) and \(q\) are distinct primes, Fermat's little theorem gives

\[p^{q-1}\equiv1\pmod q,\qquad q^{p-1}\equiv1\pmod p.\]

Now subtract the candidate remainder \(p+q\):

\[p^q+q^p-(p+q)=p\bigl(p^{q-1}-1\bigr)+q\bigl(q^{p-1}-1\bigr).\]

The first summand is divisible by \(pq\), because its bracket is divisible by \(q\). The second is also divisible by \(pq\), because its bracket is divisible by \(p\). Hence \(p^q+q^p\equiv p+q\pmod{pq}\).

It remains to check the range. We have \(p+q>0\), and

\[pq-p-q=(p-1)(q-1)-1>0.\]

Thus \(p+q\) is already the least nonnegative remainder.

\[\boxed{p+q}\]

Solution 2. Determine two remainders separately

Let \(N=p^q+q^p\). Modulo \(p\), the first term vanishes and Fermat gives \(q^p\equiv q\). Modulo \(q\), the second term vanishes and Fermat gives \(p^q\equiv p\). Therefore

\[N\equiv q\pmod p,\qquad N\equiv p\pmod q.\]

The number \(p+q\) has exactly these same two remainders. Hence both \(p\) and \(q\) divide \(N-(p+q)\). Since they are coprime, \(pq\) divides this difference. The range check above again shows that the remainder is \(p+q\).

Problem 14. Modular inverse using Fermat's little theorem

If \(p\) is prime and \(a\not\equiv0\pmod p\), Fermat's little theorem gives

\[a^{p-1}\equiv1\pmod p\quad\Rightarrow\quad a^{p-2}\equiv a^{-1}\pmod p.\]

Compute the modular inverse of \(a\) modulo \(p\) by evaluating \(a^{p-2}\bmod p\) using binary exponentiation, also called exponentiation by squaring.

Task

  • Read two integers \(a\) and \(p\) from standard input.
  • Assume \(p\) is prime and \(a\bmod p\ne0\).
  • Print \(a^{-1}\bmod p\), computed as \(a^{p-2}\bmod p\) with binary exponentiation.

Input format

Two integers \(a\) and \(p\) separated by a space.

Output format

A single integer: the modular inverse of \(a\) modulo \(p\).

Constraints

  • \(p\) is prime.
  • \(a\bmod p\ne0\), so the inverse exists.
  • Use iterative binary exponentiation, taking the remainder modulo \(p\) after every multiplication and squaring.
  • Target complexity: \(O(\log p)\) time and \(O(1)\) extra memory, measured in arithmetic operations and integer variables.

Examples

ExampleInputOutput
13 75
22 53
310 1712

In Example 1, \(3\cdot5\equiv1\pmod7\).

Notes

  • While the exponent \(e>0\), multiply the result by the current base if \(e\) is odd; square the base; replace \(e\) by its integer quotient on division by \(2\). Reduce each product modulo \(p\).
  • Read from stdin and write to stdout.
Show solution and code

Solution. Iterative binary exponentiation

Maintain a running product, a current base, and the exponent still to be processed. An odd exponent contributes one copy of the current base to the product. Squaring the base then lets us halve the remaining exponent.

Python 3 implementation

import sys

def inverse_mod(a, p):
    result = 1
    base = a % p
    exponent = p - 2

    while exponent > 0:
        if exponent % 2 == 1:
            result = (result * base) % p
        base = (base * base) % p
        exponent //= 2

    return result

def main():
    a, p = map(int, sys.stdin.buffer.read().split())
    print(inverse_mod(a, p))

if __name__ == "__main__":
    main()

The code performs the exponentiation explicitly. It does not use a built-in power or inverse function. Reducing \(a\) initially also handles negative inputs and inputs larger than \(p\).

Trace for Example 1

For \(a=3\) and \(p=7\), the exponent is \(p-2=5\). Each row shows the state at a loop test.

Stageexponentbaseresult
Initially531
After iteration 1223
After iteration 2143
After iteration 3025

Why the algorithm works

At every loop test, the stored values \(r\), \(b\), and \(e\), standing for result, base, and exponent, satisfy

\[r\,b^e\equiv a^{p-2}\pmod p.\]

This relation holds initially because \(r=1\), \(b\equiv a\pmod p\), and \(e=p-2\). To see why an iteration preserves it, consider the two possible forms of \(e\):

  • If \(e=2k\), the new values represent \(r(b^2)^k=rb^e\).
  • If \(e=2k+1\), the new values represent \((rb)(b^2)^k=rb^e\).

Taking remainders after multiplication does not change these congruences. Repeated halving eventually gives \(e=0\), so the final result satisfies \(r\equiv a^{p-2}\pmod p\). Fermat's little theorem now gives

\[\boxed{ar\equiv a^{p-1}\equiv1\pmod p.}\]

The program keeps \(0\le r<p\), so it prints the required representative of the inverse.

Complexity and the smallest prime

Each iteration halves the exponent and uses at most two modular multiplications. Hence the algorithm uses \(O(\log p)\) modular operations and \(O(1)\) extra integer variables, as required in the standard arithmetic-operation model.

For \(p=2\), the exponent is \(0\), so the loop is skipped and the output is \(1\). This is correct: the only nonzero residue modulo \(2\) is \(1\), whose inverse is itself.