欧拉函数

Euler's Totient Function.

Definition. Euler's Totient Function φ(N)\varphi(N) is defined as the number of positive integers ≤N\leq N that are relatively prime to NN, where 11 is counted as being relatively prime to all integers.

The general case of Euler's Totient is φ(n)=nΠp∣n(1−1p)=n(1−1p1)(1−1p2)...(1−1pr)\varphi(n)=n\Pi_{p|n}(1-\frac{1}{p})=n(1-\frac{1}{p_1})(1-\frac{1}{p_2})...(1-\frac{1}{p_r}). This is essentially the Inclusion-Exclusion principle.

Code Implementation: Linear Sieve

To calculate a specific totient of nn, we could design a trivial O(nlog⁡n)O(n\log n) algorithm based on the above formula. However, there exists a sieve algorithm that could calculate totients of number 11 to nn in linear time O(n)O(n). Below is the code in C++:

C++
// the Euler sieve
// bool array: is_prime, int array: prime, phi

void sieve() {
    for (int i = 1; i < N; ++i)	is_prime[i] = 1;
    cnt = 0;
    is_prime[1] = 0;
    phi[1] = 1;
    for (int i = 2; i <= N; ++i) {
        if (is_prime[i]) {
            prime[++cnt] = i;
            phi[i] = i - 1;     // for a prime p, phi[p] = p - 1
        }
        for (int j = 1; j <= cnt && i * prime[j] <= N; ++j) {
            is_prime[i * prime[j]] = 0;
            if (i % prime[j])
                phi[i * prime[j]] = phi[i] * phi[prime[j]];
	else {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
        }
    }
}

Reference & Explanation: (OI Wiki) Linear Sieve for Euler's totient function

Euler's Theorem

Theorem. Let nn be a positive integer, and let aa be an integer that is relatively prime to nn. Then aφ(n)≡1(modn)a^{\varphi(n)}\equiv 1\pmod{n}, where φ(n)\varphi(n) is Euler's totient function of nn.

Since gcd⁡(a,n)=1\gcd(a,n)=1, aa is invertible modulo nn. According to Euler's Theorem, aφ(n)≡1(modn)a^{\varphi(n)}\equiv 1\pmod{n}. Therefore a×aφ(n)−1≡1(modn)a\times a^{\varphi(n)-1}\equiv 1\pmod{n}. aφ(n)−1a^{\varphi(n)-1} is a multiplicative inverse of aa modulo nn.

An informal proof. (need the knowledge of Zn∗\mathbb{Z}_n^{*}, multiplicative group of integers modulo nn).

For a∈Zn∗a\in\mathbb{Z}_n^{*}, {r1,r2,...rφ(n)}=(Z/n)∗={ar1,ar2,...arφ(n)}\{r_1,r_2,...r_{\varphi(n)}\}=(\mathbb{Z}/n)^{*}=\{ar_1,ar_2,...ar_{\varphi(n)}\}.

Therefore r1r2...rφ(n)≡(ar1)(ar2)...(arφ(n))r_1r_2...r_{\varphi(n)}\equiv (ar_1)(ar_2)...(ar_{\varphi(n)}), which reduces to 1≡aφ(n)1\equiv a^{\varphi(n)}. This cancellation is allowed since all rir_i have multiplicative inverses modulo nn.

Reference & Explanation: (Brilliant Math Wiki) Proof of Euler's Theorem

Eureka moment!

欧拉函数也接触了很长一段时间了,刻在 DNA 里的 φ∗1=Id\varphi * \mathbf{1}=\mathbf{Id}。

这里主要想讲下关于欧拉函数的一般公式:φ(n)=nΠp∣n(1−1p)\varphi(n)=n\Pi_{p|n}(1-\frac{1}{p})。本质上这是一个容斥原理的应用。

举个例子就容易理解了:假设 nn 只有两个质因子 p1,p2p_1,p_2,那么展开上式可得 φ(n)=n−np1−np2+np1p2\varphi(n)=n-\frac{n}{p_1}-\frac{n}{p_2}+\frac{n}{p_1p_2}. 用自然语言描述,即在 1−n1-n 中与 nn 互质的数的个数为 nn 减去是 p1p_1 倍数的数的个数,再减去是 p2p_2 倍数的数的个数。此时,是 p1p2p_1p_2 倍数的数将会被重复减去两次;根据容斥原理,我们将多减的个数重新加回来,因此最后再加上是 p1p2p_1p_2 倍数的数的个数。

关于欧拉函数线性筛算法的设计也十分巧妙,它是建立在质数线性筛的基础上的。具体可见 OI Wiki 中的相关解释,讲的很清楚。