{
  "id": "eulers-totient-function",
  "title": "Euler’s Totient Function",
  "lang": "en",
  "source_lang": "en",
  "summary": "Euler’s totient function counts the positive integers up to a given integer that are relatively prime to it, linking prime factorization, modular arithmetic, and cryptography.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Euler’s phi function",
    "Euler function",
    "totient function"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/eulers-totient-function/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/eulers-totient-function/",
      "json": "https://aiwiki.page/zh/eulers-totient-function.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Euler’s totient function, denoted \\(\\varphi(n)\\), is the function that counts the positive integers not exceeding a positive integer \\(n\\) that are relatively prime to \\(n\\). Two integers are relatively prime, or coprime, when their greatest common divisor is \\(1\\). A central function in number theory, the totient connects prime factorization with the structure of multiplication in modular arithmetic. It is named after Leonhard Euler, who studied it in connection with his generalization of Fermat’s little theorem."
    },
    {
      "heading": "Definition and examples",
      "text": "For every positive integer \\(n\\),\n\n\\[\\varphi(n)=\\#\\{k:1\\leq k\\leq n,\\ \\gcd(k,n)=1\\},\\]\n\nwhere \\(\\#\\) denotes the number of elements in a finite set.\n\nThe convention \\(\\varphi(1)=1\\) follows directly from this definition, since \\(\\gcd(1,1)=1\\). For \\(n>1\\), the endpoint \\(n\\) is not counted, so the definition is equivalent to counting coprime integers strictly between \\(0\\) and \\(n\\).\n\nFor example, the integers from \\(1\\) through \\(12\\) that are coprime to \\(12\\) are\n\n\\[1,\\ 5,\\ 7,\\ 11,\\]\n\nand therefore \\(\\varphi(12)=4\\).\n\nThe first twelve values are:\n\n\\(n\\) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12\n\\(\\varphi(n)\\) | 1 | 1 | 2 | 2 | 4 | 2 | 6 | 4 | 6 | 4 | 10 | 4\n\nFor \\(n>1\\), the ratio \\(\\varphi(n)/n\\) is the probability that an integer chosen uniformly from \\(1,\\ldots,n\\) is coprime to \\(n\\)."
    },
    {
      "heading": "Formula from prime factorization",
      "text": "If\n\n\\[n=p_1^{a_1}p_2^{a_2}\\cdots p_r^{a_r}\\]\n\nis the factorization of \\(n>1\\) into distinct primes \\(p_i\\), then\n\n\\[\\boxed{\\varphi(n)=n\\prod_{p\\mid n}\\left(1-\\frac1p\\right)}\\]\n\nor, equivalently,\n\n\\[\\varphi(n)=\\prod_{i=1}^{r}p_i^{a_i-1}(p_i-1).\\]\n\nThe product is over the distinct prime divisors of \\(n\\), not over their repeated occurrences. For \\(n=1\\), the empty-product convention gives the same formula.\n\nFor example,\n\n\\[360=2^3\\cdot3^2\\cdot5,\\]\n\nso\n\n\\[\\varphi(360) =360\\left(1-\\frac12\\right) \\left(1-\\frac13\\right) \\left(1-\\frac15\\right) =96.\\]\n\nWhy the formula holds\n\nAn integer is coprime to \\(n\\) exactly when it is divisible by none of the primes dividing \\(n\\). Among the integers from \\(1\\) through \\(n\\), exactly \\(n/p\\) are divisible by a prime divisor \\(p\\).\n\nThe inclusion–exclusion principle therefore gives\n\n\\[\\varphi(n) =n-\\sum_{p\\mid n}\\frac np +\\sum_{\\substack{p<q\\\\p,q\\mid n}}\\frac{n}{pq} -\\cdots,\\]\n\nwhich factors into the product formula.\n\nThus \\(\\varphi(n)/n\\) depends only on which primes divide \\(n\\), not on their exponents.\n\nPrimes and prime powers\n\nFor a prime \\(p\\),\n\n\\[\\varphi(p)=p-1,\\]\n\nbecause every integer from \\(1\\) to \\(p-1\\) is coprime to \\(p\\).\n\nMore generally, for \\(a\\geq1\\),\n\n\\[\\varphi(p^a)=p^a-p^{a-1}.\\]\n\nThe excluded integers are precisely the multiples of \\(p\\).\n\nConversely, if \\(n>1\\) and \\(\\varphi(n)=n-1\\), then \\(n\\) is prime."
    },
    {
      "heading": "Multiplicativity and divisor identities",
      "text": "The totient is a multiplicative arithmetic function:\n\n\\[\\gcd(m,n)=1 \\quad\\Longrightarrow\\quad \\varphi(mn)=\\varphi(m)\\varphi(n).\\]\n\nThe coprimality condition is essential. For example,\n\n\\[\\varphi(4)=2 \\qquad\\text{but}\\qquad \\varphi(2)\\varphi(2)=1.\\]\n\nThus the totient is not completely multiplicative.\n\nOne explanation of multiplicativity comes from the Chinese remainder theorem: when \\(m\\) and \\(n\\) are coprime, invertible residue classes modulo \\(mn\\) correspond bijectively to pairs of invertible residue classes modulo \\(m\\) and modulo \\(n\\).\n\nA fundamental divisor-sum identity is\n\n\\[\\boxed{\\sum_{d\\mid n}\\varphi(d)=n}.\\]\n\nTo see this, partition the integers \\(1,\\ldots,n\\) according to their greatest common divisor with \\(n\\). For each divisor \\(g\\mid n\\), the integers satisfying \\(\\gcd(k,n)=g\\) are exactly those of the form \\(k=gj\\) with\n\n\\[1\\leq j\\leq n/g, \\qquad \\gcd(j,n/g)=1.\\]\n\nThere are \\(\\varphi(n/g)\\) such integers, and their counts sum to \\(n\\).\n\nApplying Möbius inversion yields another useful formula:\n\n\\[\\varphi(n)=\\sum_{d\\mid n}\\mu(d)\\frac nd,\\]\n\nwhere \\(\\mu\\) is the Möbius function."
    },
    {
      "heading": "Euler’s theorem and the group of units",
      "text": "The residue classes modulo \\(n\\) that have multiplicative inverses form the group of units, written\n\n\\[(\\mathbb Z/n\\mathbb Z)^\\times.\\]\n\nA residue class is invertible exactly when its representatives are coprime to \\(n\\), so this group has \\(\\varphi(n)\\) elements.\n\nConsequently, Lagrange’s theorem from group theory gives Euler’s theorem:\n\n\\[\\gcd(a,n)=1 \\quad\\Longrightarrow\\quad a^{\\varphi(n)}\\equiv1\\pmod n.\\]\n\nFor example, \\(\\varphi(10)=4\\), and\n\n\\[3^4=81\\equiv1\\pmod{10}.\\]\n\nWhen \\(n=p\\) is prime, Euler’s theorem becomes\n\n\\[a^{p-1}\\equiv1\\pmod p \\qquad (p\\nmid a),\\]\n\nwhich is Fermat’s little theorem.\n\nThe totient gives the size of the group of units, but not necessarily the smallest exponent that works for every element. That exponent is the Carmichael function \\(\\lambda(n)\\), which divides \\(\\varphi(n)\\). For instance,\n\n\\[\\varphi(8)=4,\\qquad \\lambda(8)=2,\\]\n\nbecause every odd integer has square congruent to \\(1\\) modulo \\(8\\)."
    },
    {
      "heading": "Further interpretations and applications",
      "text": "Reduced fractions\n\nFor \\(n>1\\), \\(\\varphi(n)\\) is the number of fractions\n\n\\[\\frac an,\\qquad 1\\leq a<n,\\]\n\nthat are already in lowest terms. The divisor identity above can therefore also be understood by reducing fractions and grouping them by their reduced denominators.\n\nCyclotomic polynomials\n\nA complex number is a primitive \\(n\\)th root of unity if its multiplicative order is exactly \\(n\\). There are \\(\\varphi(n)\\) such roots. Hence the cyclotomic polynomial \\(\\Phi_n(x)\\), whose roots are precisely those primitive roots, has degree\n\n\\[\\deg\\Phi_n=\\varphi(n).\\]\n\nThis connects the totient to polynomials, complex numbers, and the study of field extensions.\n\nRSA cryptography\n\nIn the RSA cryptosystem, a modulus is commonly constructed as\n\n\\[N=pq,\\]\n\nwhere \\(p\\) and \\(q\\) are distinct primes. Then\n\n\\[\\varphi(N)=(p-1)(q-1).\\]\n\nA standard derivation chooses exponents \\(e,d\\) satisfying\n\n\\[ed\\equiv1\\pmod{\\varphi(N)}.\\]\n\nEuler’s theorem explains why the corresponding exponentiations undo one another on invertible residue classes. Correctness on all residue classes modulo this squarefree modulus follows by considering the congruences modulo \\(p\\) and \\(q\\) separately. RSA may also use the condition modulo \\(\\lambda(N)\\).\n\nFor a product of two distinct primes, knowing \\(N\\) and \\(\\varphi(N)\\) is enough to recover the factors:\n\n\\[p+q=N-\\varphi(N)+1.\\]\n\nThe primes are then the roots of\n\n\\[x^2-\\bigl(N-\\varphi(N)+1\\bigr)x+N=0.\\]\n\nThus the totient of an RSA modulus is sensitive information."
    },
    {
      "heading": "Computation",
      "text": "When the distinct prime divisors of \\(n\\) are known, the product formula makes evaluation straightforward:\n\n- Set \\(r=n\\).\n- For each distinct prime divisor \\(p\\), replace \\(r\\) by \\(r-r/p\\).\n- Return \\(r\\).\n\nFor a single integer, trial division can find the required primes using \\(O(\\sqrt n)\\) trial divisions in a simple implementation. For large inputs, factorization may dominate the work; this bound concerns the numeric value of \\(n\\), rather than its bit length.\n\nTo compute every value up to a bound \\(X\\), a sieve initializes \\(T[k]=k\\), then updates every multiple \\(k\\) of each prime \\(p\\) by\n\n\\[T[k]\\leftarrow T[k]-\\frac{T[k]}p.\\]\n\nThe standard sieve takes \\(O(X\\log\\log X)\\) arithmetic operations and \\(O(X)\\) storage, expressed in big-O notation. Linear-sieve variants can compute the table in \\(O(X)\\) arithmetic operations."
    },
    {
      "heading": "Size and average behavior",
      "text": "For every \\(n>1\\),\n\n\\[1\\leq\\varphi(n)\\leq n-1.\\]\n\nMoreover, \\(\\varphi(n)\\) is even whenever \\(n>2\\): the coprime residues pair as \\(a\\) and \\(n-a\\), with no fixed point.\n\nThe function is not monotone. For example,\n\n\\[\\varphi(5)=4>\\varphi(6)=2.\\]\n\nIts average growth is described by\n\n\\[\\sum_{n\\leq x}\\varphi(n) =\\frac{3}{\\pi^2}x^2+O(x\\log x).\\]\n\nThus the average of its first \\(x\\) values is asymptotic to \\(3x/\\pi^2\\).\n\nA related but different average is\n\n\\[\\frac1x\\sum_{n\\leq x}\\frac{\\varphi(n)}n \\longrightarrow\\frac6{\\pi^2}.\\]\n\nThe same constant is the limiting probability that two independently and uniformly chosen integers from \\(1,\\ldots,X\\) are coprime as \\(X\\) tends to infinity.\n\nIndividually, the ratio \\(\\varphi(n)/n\\) can be arbitrarily close to \\(1\\), as along large primes, and arbitrarily close to \\(0\\), as along products of increasingly many distinct small primes. The function therefore has substantial relative fluctuations despite its regular average behavior."
    },
    {
      "heading": "Inverse questions and open problems",
      "text": "Not every positive integer is a totient value. Since \\(\\varphi(n)\\) is even for \\(n>2\\), the only odd value it takes is \\(1\\). Some even integers, such as \\(14\\), are also absent from its range.\n\nDifferent inputs can give the same output:\n\n\\[\\varphi(3)=\\varphi(4)=\\varphi(6)=2.\\]\n\nCarmichael’s totient conjecture asserts that no positive integer occurs as a totient value exactly once: whenever \\(\\varphi(n)=m\\), there is some \\(n'\\ne n\\) with \\(\\varphi(n')=m\\).\n\nLehmer’s totient problem asks whether a composite integer \\(n\\) can satisfy\n\n\\[\\varphi(n)\\mid n-1.\\]\n\nEvery prime satisfies this divisibility condition, but no composite example is known."
    }
  ],
  "links": [
    "mathematical-function",
    "greatest-common-divisor",
    "number-theory",
    "modular-arithmetic",
    "leonhard-euler",
    "integer",
    "probability",
    "prime-number",
    "inclusion-exclusion-principle",
    "chinese-remainder-theorem",
    "mobius-function",
    "lagranges-theorem-group-theory",
    "group-theory",
    "fermats-little-theorem",
    "polynomial",
    "complex-number",
    "field-extension",
    "rsa-cryptosystem",
    "big-o-notation"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}