{
  "id": "fermats-little-theorem",
  "title": "Fermat's Little Theorem",
  "lang": "en",
  "source_lang": "en",
  "summary": "Fermat’s little theorem states that if p is prime, then a raised to the pth power is congruent to a modulo p for every integer a.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Fermat’s theorem on congruences"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/fermats-little-theorem/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/fermats-little-theorem/",
      "json": "https://aiwiki.page/zh/fermats-little-theorem.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Fermat’s little theorem is a fundamental result in number theory describing powers of integers modulo a prime number. It states that, for every prime \\(p\\) and every integer \\(a\\),\n\n\\[a^p\\equiv a\\pmod p.\\]\n\nEquivalently, if \\(p\\) does not divide \\(a\\), then\n\n\\[a^{p-1}\\equiv1\\pmod p.\\]\n\nNamed after Pierre de Fermat, the theorem is a basic tool in modular arithmetic, with applications to divisibility, the computation of large powers, modular inverses, and tests for primality. Its converse is false: some composite integers satisfy the corresponding congruence for particular bases, and some do so for every base relatively prime to them."
    },
    {
      "heading": "Statement and interpretation",
      "text": "The notation \\(x\\equiv y\\pmod p\\) means that \\(p\\) divides \\(x-y\\). Thus the theorem’s first form says that\n\n\\[p\\mid(a^p-a)\\]\n\nfor every integer \\(a\\).\n\nThe two common forms have slightly different hypotheses:\n\n- Universal form: If \\(p\\) is prime, then \\(a^p\\equiv a\\pmod p\\) for every integer \\(a\\).\n- Nonzero-residue form: If \\(p\\) is prime and \\(p\\nmid a\\), then \\(a^{p-1}\\equiv1\\pmod p\\).\n\nThe restriction \\(p\\nmid a\\) in the second form is essential. If \\(p\\mid a\\), then \\(a^{p-1}\\equiv0\\pmod p\\), not \\(1\\).\n\nTo pass from the universal form to the nonzero-residue form, one cancels \\(a\\) modulo \\(p\\); this is valid because \\(a\\) is relatively prime to \\(p\\). Conversely, multiplying the nonzero-residue form by \\(a\\) gives the universal form when \\(p\\nmid a\\), while the case \\(p\\mid a\\) is immediate.\n\nFor example, taking \\(p=7\\) and \\(a=3\\) gives\n\n\\[3^6=729\\equiv1\\pmod7,\\]\n\nand consequently\n\n\\[3^7\\equiv3\\pmod7.\\]"
    },
    {
      "heading": "History",
      "text": "Fermat stated the result in a letter to Bernard Frénicle de Bessy dated 18 October 1640, without supplying a proof. Leonhard Euler later published a proof in the eighteenth century and developed the more general result now known as Euler’s theorem.\n\nThe adjective “little” distinguishes this result from Fermat’s Last Theorem, which concerns the absence of positive integer solutions to \\(x^n+y^n=z^n\\) for integers \\(n>2\\). Despite their shared attribution, the two theorems address different problems and require very different methods of proof."
    },
    {
      "heading": "Proofs",
      "text": "Permutation of nonzero residues\n\nAssume \\(p\\) is prime and \\(p\\nmid a\\). Consider the nonzero residues modulo \\(p\\):\n\n\\[1,2,\\ldots,p-1.\\]\n\nMultiplying each by \\(a\\) produces\n\n\\[a,2a,\\ldots,(p-1)a.\\]\n\nNone is congruent to zero modulo \\(p\\). Moreover, no two are congruent: if\n\n\\[ai\\equiv aj\\pmod p,\\]\n\nthen \\(p\\mid a(i-j)\\). Since \\(p\\nmid a\\), it follows that \\(p\\mid(i-j)\\), which forces \\(i=j\\) when \\(1\\le i,j\\le p-1\\).\n\nThus multiplication by \\(a\\) gives a permutation of the nonzero residues. Their products are therefore congruent:\n\n\\[a^{p-1}(p-1)!\\equiv(p-1)!\\pmod p.\\]\n\nHere \\((p-1)!\\) is a factorial. None of its factors is divisible by \\(p\\), so it can be cancelled modulo \\(p\\), yielding\n\n\\[a^{p-1}\\equiv1\\pmod p.\\]\n\nThis proof highlights a key property of prime moduli: every nonzero residue has a multiplicative inverse.\n\nBinomial proof\n\nFor a prime \\(p\\), every binomial coefficient\n\n\\[\\binom pk,\\qquad 1\\le k\\le p-1,\\]\n\nis divisible by \\(p\\). The binomial theorem therefore gives\n\n\\[(x+y)^p\\equiv x^p+y^p\\pmod p.\\]\n\nSetting \\(y=1\\) yields\n\n\\[(x+1)^p\\equiv x^p+1\\pmod p.\\]\n\nStarting from \\(0^p\\equiv0\\pmod p\\), mathematical induction establishes \\(a^p\\equiv a\\pmod p\\) for every nonnegative integer \\(a\\). Every integer is congruent modulo \\(p\\) to one of \\(0,1,\\ldots,p-1\\), so the statement follows for all integers.\n\nGroup-theoretic proof\n\nThe nonzero residues modulo \\(p\\) form a multiplicative group with \\(p-1\\) elements. In group theory, Lagrange’s theorem implies that the order of any element divides the number of elements in a finite group. Hence every nonzero residue satisfies\n\n\\[a^{p-1}=1\\]\n\nin that group.\n\nThis presents Fermat’s little theorem as a special case of a general fact about finite groups."
    },
    {
      "heading": "Applications",
      "text": "Reducing large exponents\n\nIf \\(p\\nmid a\\), exponents can be reduced modulo \\(p-1\\). Writing\n\n\\[n=q(p-1)+r,\\qquad 0\\le r<p-1,\\]\n\ngives\n\n\\[a^n\\equiv a^r\\pmod p.\\]\n\nFor example, because \\(1000=166\\cdot6+4\\),\n\n\\[3^{1000}\\equiv3^4=81\\equiv4\\pmod7.\\]\n\nThis reduction does not apply without qualification when \\(p\\mid a\\). In that case, every positive power of \\(a\\) is congruent to zero modulo \\(p\\); reducing a positive exponent to zero would incorrectly replace it by \\(a^0=1\\).\n\nComputing modular inverses\n\nFor \\(p\\nmid a\\), the theorem gives\n\n\\[a\\cdot a^{p-2}\\equiv1\\pmod p.\\]\n\nThus the multiplicative inverse of \\(a\\) modulo \\(p\\) is\n\n\\[a^{-1}\\equiv a^{p-2}\\pmod p.\\]\n\nFor instance,\n\n\\[3^{-1}\\equiv3^5\\equiv5\\pmod7,\\]\n\nconsistent with \\(3\\cdot5=15\\equiv1\\pmod7\\).\n\nThe power can be computed efficiently by repeated squaring. Another method for finding modular inverses uses the extended Euclidean algorithm, which also works for composite moduli whenever the number and modulus are relatively prime.\n\nPrimality testing\n\nThe theorem provides a necessary condition for primality. If an integer \\(n>1\\) has a base \\(a\\), relatively prime to \\(n\\), for which\n\n\\[a^{n-1}\\not\\equiv1\\pmod n,\\]\n\nthen \\(n\\) is composite.\n\nThis is the basis of the Fermat primality test. A base that violates the congruence is a conclusive witness to compositeness. A base that satisfies it, however, does not prove that \\(n\\) is prime.\n\nModular exponentiation makes such tests practical even for large integers. Stronger methods, including the Miller–Rabin primality test, examine additional consequences of prime-modulus arithmetic to detect composites that a Fermat test may miss."
    },
    {
      "heading": "The failure of the converse",
      "text": "Fermat pseudoprimes\n\nA composite integer \\(n\\) satisfying\n\n\\[a^{n-1}\\equiv1\\pmod n\\]\n\nfor a specified base \\(a\\) relatively prime to \\(n\\) is called a Fermat pseudoprime to base \\(a\\).\n\nFor example,\n\n\\[341=11\\cdot31\\]\n\nis composite, but\n\n\\[2^{10}=1024\\equiv1\\pmod{341}.\\]\n\nSince \\(340=34\\cdot10\\), this implies\n\n\\[2^{340}\\equiv1\\pmod{341}.\\]\n\nThus \\(341\\) passes the Fermat test to base \\(2\\).\n\nCarmichael numbers\n\nA Carmichael number is a composite positive integer \\(n\\) for which\n\n\\[a^{n-1}\\equiv1\\pmod n\\]\n\nfor every integer \\(a\\) relatively prime to \\(n\\). The smallest example is\n\n\\[561=3\\cdot11\\cdot17.\\]\n\nConsequently, testing additional relatively prime bases cannot make the Fermat congruence alone a definitive primality criterion. A base sharing a nontrivial factor with \\(n\\) can reveal compositeness through a greatest common divisor calculation, but that is distinct from passing or failing the coprime-base congruence."
    },
    {
      "heading": "Generalizations and algebraic meaning",
      "text": "Euler’s theorem\n\nEuler’s theorem extends the result from prime moduli to arbitrary positive integer moduli \\(n\\):\n\n\\[a^{\\varphi(n)}\\equiv1\\pmod n \\qquad\\text{if }\\gcd(a,n)=1,\\]\n\nwhere \\(\\varphi(n)\\), Euler’s totient function, counts the residue classes modulo \\(n\\) that are relatively prime to \\(n\\).\n\nFor a prime \\(p\\), every integer from \\(1\\) through \\(p-1\\) is relatively prime to \\(p\\), so \\(\\varphi(p)=p-1\\), recovering Fermat’s little theorem.\n\nThe exponent \\(p-1\\) need not be the smallest positive exponent producing \\(1\\). The smallest such exponent for a particular \\(a\\), called its multiplicative order modulo \\(p\\), divides \\(p-1\\). For example, \\(2^3\\equiv1\\pmod7\\), so the order of \\(2\\) modulo \\(7\\) is \\(3\\), rather than \\(6\\).\n\nFinite fields\n\nThe residue classes modulo \\(p\\) form a finite field, denoted \\(\\mathbb F_p\\). Fermat’s little theorem says that every element of this field satisfies\n\n\\[x^p=x.\\]\n\nMore generally, in any finite field with \\(q\\) elements,\n\n\\[x^q=x\\]\n\nfor every element \\(x\\), because its nonzero elements form a multiplicative group of order \\(q-1\\).\n\nThere is an important distinction between these statements. In a field with \\(p^m\\) elements, the map \\(x\\mapsto x^p\\) is an automorphism, called the Frobenius automorphism, but it need not fix every element when \\(m>1\\). It is the map \\(x\\mapsto x^{p^m}\\) that fixes every element of that field."
    }
  ],
  "links": [
    "number-theory",
    "integer",
    "prime-number",
    "pierre-de-fermat",
    "modular-arithmetic",
    "leonhard-euler",
    "fermats-last-theorem",
    "permutation",
    "factorial",
    "binomial-coefficient",
    "binomial-theorem",
    "mathematical-induction",
    "group-theory",
    "lagranges-theorem-group-theory",
    "euclidean-algorithm",
    "miller-rabin-primality-test",
    "greatest-common-divisor",
    "eulers-theorem-number-theory",
    "finite-field"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}