This page discusses Fermat's little theorem, Euler's theorem, and their extensions. These theorems solve the problem of computing powers with arbitrarily large exponents under arbitrary moduli.
Fermat's little theorem
Fermat's little theorem is one of the most fundamental theorems in number theory. It is also the theoretical basis of the Fermat primality test.
Fermat's little theorem
Let be a prime. For every integer with , we have .
Theorem
Let be a prime. For every integer , we have .
These two congruences are equivalent when ; when , holds trivially. Therefore, the two statements are equivalent. Both are often called Fermat's little theorem.
Proof 1
Let be prime and let . We first prove that for , the remainders are all distinct. Suppose otherwise. If there were such that
But neither nor is a multiple of , which is a contradiction.
In other words, these remainders form a permutation of . Hence,
This implies
In other words, the left-hand side is a multiple of . However, none of is a multiple of , so the only possibility is ; that is, Fermat's little theorem holds.
Proof 2
The second formulation of Fermat's little theorem holds for every , so we can use mathematical induction. The case of negative integers is easily reduced to that of nonnegative integers.
The base case is , which is obvious. Assume that the statement holds for ; we need to prove that it also holds for . By the binomial theorem,
Except for the first and last terms, in the expression for a binomial coefficient, divides the numerator but does not divide the denominator. Thus, for , these coefficients are multiples of . Therefore,
The second congruence uses the induction hypothesis. Thus, by mathematical induction, Fermat's little theorem holds.
The converse of Fermat's little theorem is false. Even if for every coprime to we have , need not be prime. See the Fermat primality test section for related discussion.
Euler's theorem
Euler's theorem generalizes Fermat's little theorem to arbitrary moduli, while still requiring the base and modulus to be coprime.
Euler's theorem
For an integer and an integer with , we have , where is the Euler totient function.
Proof
As in the proof of Fermat's little theorem, take a sequence of numbers coprime to and operate on it. Consider the set
This is a reduced residue system modulo . By the definition of the Euler totient function, . As above, multiplying its elements by merely permutes the set:
Indeed, it is easy to verify that , and that distinct give distinct values of and . Hence,
Repeating the preceding argument and canceling gives .
For a prime , , so Fermat's little theorem is a special case of Euler's theorem. In general, the exponent in Euler's theorem is not the smallest exponent for which the congruence holds. It can be improved to , where is the Carmichael function. For the algebraic background, see the multiplicative group of integer residue classes.
Extended Euler's theorem
Extended Euler's theorem1 further generalizes the result to the case where the base and modulus are not coprime. It completely solves the problem of computing powers of arbitrary bases under arbitrary moduli by reducing them to exponents smaller than , which can then be computed in time using binary exponentiation.
Extended Euler's theorem
For every positive integer , integer , and nonnegative integer ,
The second case says that if , no further exponent reduction is needed and binary exponentiation can be applied directly. The main difference between the third and first cases is whether an additional must be added after reducing the exponent modulo the period. Of course, it is also correct to merge the first case into the second and third cases.
Intuitive understanding
Before proving the theorem rigorously, we can first develop an intuitive understanding of its meaning.
Consider how the remainder changes as increases. The remainder always lies in , while there are infinitely many values of . Regard as a directed edge between remainder nodes. A cycle like the one shown in the figure must therefore arise.
Extended Euler's theorem says that these cycles may be pure cycles (the first case) or lasso-shaped cycles (the second and third cases). In a pure cycle, no node has two predecessors, whereas such nodes appear in a lasso-shaped cycle. Thus, in general, it is enough to determine the cycle length and the length before entering the cycle to reduce the exponent using this property.
Rigorous proof
This section gives a rigorous proof of extended Euler's theorem.
Proof
First, we show that there exists such that the integer is coprime to . Let denote, for an integer , the exponent of the prime in its prime factorization. We may take
Every power of a prime common to and is then already contained in , so is coprime to the remaining factor of , namely .
Now, for , consider the congruence
Since , dividing both sides of the congruence (including the modulus) by gives
Since is coprime to the modulus , Euler's theorem can be applied directly, giving
Multiplying back by yields
This is the form of extended Euler's theorem. The formula shows that the cycle length is , while the length before entering the cycle is .
The parameters obtained here are tighter than those in the statement of extended Euler's theorem, but are relatively difficult to compute. We can show that they can be relaxed to the parameters in that theorem. First, from the formula for the Euler totient function, since , we have . In other words, is also a cycle length. Second, can be relaxed to . For every and every ,
The inequality in the second line follows from the binomial expansion, retaining only the constant and linear terms. Therefore,
This completes the proof of the stated result.
Example
This section demonstrates a classic application of extended Euler's theorem: computing a power tower under an arbitrary modulus. A power tower is an expression of the form , where is Knuth's arrow notation and are a sequence of nonnegative integers.
Library Checker - Tetration Mod
There are test cases. In each test case, given , compute . Here, denotes a power tower consisting of copies of . Formally,
We define .
Solution
The definition of allows us to compute it recursively. To compute , we only need to apply extended Euler's theorem and compute . Since holds for all , the recursion always finishes within steps. Because extended Euler's theorem is used, we must distinguish whether the current result is strictly smaller than the current modulus. This only requires one additional check when taking the remainder. Boundary cases also require attention.
Reference code
#include <iostream>
// Calculate Euler's totient for n.
int phi(int n) {
int res = n;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
res = res / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) res = res / n * (n - 1);
return res;
}
// Find remainder as in the exponent of extended Euler theorem.
int mod(long long v, int m) { return v >= m ? v % m + m : v; }
// Modular power.
int pow(int a, int b, int m) {
long long res = 1, po = a;
for (; b; b >>= 1) {
if (b & 1) res = mod(res * po, m);
po = mod(po * po, m);
}
return res;
}
// Modular tetration.
int tetra(int a, int b, int m) {
if (a == 0) return !(b & 1);
if (b == 0 || m == 1) return 1;
if (b == 1) return mod(a, m);
return pow(a, tetra(a, b - 1, phi(m)), m);
}
int main() {
int t;
std::cin >> t;
for (; t; --t) {
int a, b, m;
std::cin >> a >> b >> m;
std::cout << (tetra(a, b, m) % m) << std::endl;
}
}
Exercises
- Luogu P5091 Extended Euler's theorem template
- Codeforces 906 D. Power Tower
- Luogu P3747 [Six Provinces Joint Examination 2017] Meeting Is a Greeting
- Luogu P4139 The Correct Use of God and Sets
- Luogu P3934 [Ynoi Easy Round 2016] Zha Bo Long I
- Luogu P6736 "Wdsr-2" Bai Ze Education
References and notes
- Fermat's little theorem - Wikipedia
- Euler's theorem - Wikipedia
- Hardy, Godfrey Harold, and Edward Maitland Wright. An introduction to the theory of numbers. Oxford university press, 1979.
Footnotes
-
This name is used mainly in algorithm-contest circles rather than being the general name of the result. ↩