Note 2020.09.22 A Bit of Number Theory

This article is supported by Xu Jintao. Prime determination, finding the number of factors, linear sieve, finding the smallest prime factor, Euler's totient function, linear sieve for Euler's totient function. Determination of prime numbers: How to determine whether a number xx is prime? (single query) Brute force: use numbers in [2,N][2,\lceil\sqrt{N}\rceil] to test xx. If there exists a yy in the interval such that yxy|x, then xx is composite; otherwise, it is prime. Complexity ONO\sqrt{N}. Unique prime factorization: Obviously, an integer can be decomposed into the product of several primes pp,

This article is supported by Xu Jintao.

Prime determination Finding the number of factors Linear sieve Finding the smallest prime factor Euler's totient function Linear sieve for Euler's totient function のののののの

Determination of prime numbers

How to determine whether a number xx is prime? (single query)

Brute force: use numbers in [2,N][2,\lceil\sqrt{N}\rceil] to test xx. If there exists a yy in the interval such that yxy|x, then xx is composite; otherwise, it is prime.

Complexity O(N)O(\sqrt{N})

Unique prime factorization

Obviously, an integer can be decomposed into the product of several primes pp, i.e., the unique prime factorization of a number: x=pikix= \prod{p_{i}^{k_i}}

Finding the unique prime factorization

So how do we find the prime factorization of a number? (At this point we do not know which primes there are.)

Similar to the method in Prime determination, use trial division on xx in ascending order with complexity O(N)O(\sqrt{N}). If there exists yy such that yxy|x, let x=x÷yx=x \div y. If still yxy|x, continue doing so. For the same yy, count the number of times it appears.

Intuitively, we can see that each kk obtained is necessarily a prime pp, and the counted number of occurrences is the exponent kk of this prime.

Proof: If there exists a yy that can be decomposed into two (or more) primes p1,p2......p_1,p_2......, then for any such pp, we must have p<yp<y. If so, this prime pp must have appeared earlier.

Complexity O(N)O(\sqrt{N})

How many factors? I

How to find the number of factors of a number?

Represent the primes after decomposing a number xx as a multiset PP. The product yy of the elements in any non-empty subset SPS \in P is a factor of xx.

Consider the number of times a certain prime factor pp of xx is used. The total number of factors is ki+1\prod{k_i+1}.

Using the above method for prime factorization, we can obtain each exponent.

Complexity O(N)O(\sqrt{N})

Linear sieve

Find in O(N)O(N) whether each number in [1,N][1,N] is prime.

Using the property of "unique prime factorization", we try to make each number be updated only once:

  1. If a number has not been marked, this number is prime.
  2. For each number xx, we traverse the prime list pp once and mark xpix·p_i.

Preprocessing O(N)O(N), query O(1)O(1).

Smallest prime factor

Find the smallest prime factor of a number. It seems unrelated to the linear sieve, but it can indeed be solved with the linear sieve.

Consider maintaining during the linear sieve an fif_i representing the smallest prime factor of ii (pp is some prime): {fi=i(i=p)fi=fj(i=j×p)\left\{ \begin{aligned} f_i &= i & (i = p) \\ f_i &= f_j & (i = j \times p) \end{aligned} \right.

Preprocessing O(N)O(N), query O(1)O(1).

How many factors? II

We are already familiar with this problem. This time we also consider performing unique prime factorization on a number.

Now we can already obtain the smallest prime factor of each number in linear time. We can use it to decompose a number xx by repeatedly setting x=x÷fxx=x \div f_x. (fif_i represents the smallest prime factor of ii)

Obviously the primes we obtain are in ascending order (smallest prime factor), so the exponents are also convenient to count.

Prove that the complexity of this algorithm is not very high. Consider two questions.

What is the maximum number of prime factors of a number?

Make the prime factors as small as possible, assume they are all 22. It can be found that the maximum number of prime factors of xx is on the order of about log2xlog_2x.

What is the maximum number of factors of a number?

A relatively obvious conclusion: when there are more distinct prime factors, the number of factors is larger. We can brute-force enumerate each prime in ascending order and multiply them together pi\prod{p_i}. It can be found that this number grows very quickly. When i=9i=9, this number already exceeds 10910^9.

Preprocessing O(N)O(N), query O(log2x)O(log_2x).

Euler's totient function

Definition: Euler's totient function φ(x)φ(x) represents the number of numbers less than xx that are coprime to xx.

Multiplicativity of Euler's totient function

Definition: For f(x)f(x), if when (x,y)=1(x,y)=1, f(xy)=f(x)f(y)f(xy)=f(x)f(y), then f(x)f(x) is a multiplicative function.

As is well known, φφ is a multiplicative function, but why is it multiplicative?

a If pp is prime, ϕ(p)=p1\phi(p)=p-1

The conclusion is obvious.

b If pp is prime, φ(pk)=pkpk1φ(p^k)=p^k - p^{k-1}

Consider inclusion-exclusion: subtract the multiples of pp from the pkp^k positive integers. Because there is only one prime factor pp, all multiples of pp are multiples of pkp^k, and other numbers are not multiples of pkp^k. There are pk÷p=pk1p^k \div p = p^{k-1} such numbers.

c If p1,p2p_1,p_2 are primes, φ(p1k1p2k2)=φ(p1k1)φ(p2k2)φ(p_{1}^{k_1}·p_{2}^{k_2})=φ(p_{1}^{k_1})·φ(p_{2}^{k_2})

Similar to bb, we consider inclusion-exclusion. Because (p1k1,p2k2)=1(p_{1}^{k_1},p_{2}^{k_2})=1, the overlapping part is only multiples of p1p2p_1p_2. The formula is:

ϕ(p1k1p2k2)=p1k1p2k2p1k11p2k2p1k1p2k21+p1k11p2k21\phi(p_{1}^{k_1}·p_{2}^{k_2})=p_{1}^{k_1}·p_{2}^{k_2}-p_{1}^{k_1-1}·p_{2}^{k_2}-p_{1}^{k_1}·p_{2}^{k_2-1}+p_{1}^{k_1-1}·p_{2}^{k_2-1}

Using bb, we can separately find φ(p1k1)=p1k1p1k11φ(p_{1}^{k_1})=p_{1}^{k_1}-p_{1}^{k_1-1} and φ(p2k2)=p2k2p2k21φ(p_{2}^{k_2})=p_{2}^{k_2}-p_{2}^{k_2-1}.

Obviously, multiplying them equals the above formula, so it is proved.

d Euler's totient function φφ is multiplicative

Consider extending cc to the case of multiple prime factors pp, doing similar inclusion-exclusion. Factoring the formula obtained by inclusion-exclusion gives the following formula: φ(x)=piki1(pi1)φ(x)=\prod{p_{i}^{k_i-1}(p_i-1)}

How to find φφ

Factoring out the common factor from the formula in part dd above gives φ(x)=xpx(11p)φ(x)=x\prod_{p|x}{(1- \frac{1}{p})}

Then we use the method in Finding the unique prime factorization to obtain each prime factor and substitute it into the formula.

Complexity O(N)O(\sqrt{N})

Linear sieve for Euler's totient function

Of course we can also find the unique prime factorization through the linear sieve, but this time we have an even more powerful algorithm.

For a prime pp and a number kk with (p,k)>1(p,k)>1, φ(pk)=pφ(k)φ(pk)=p·φ(k)

Consider conclusion cc in Multiplicativity of Euler's totient function: φ(p1k1p2k2)=p1k1p2k2p1k11p2k2p1k1p2k21+p1k11p2k21φ(p_{1}^{k_1}·p_{2}^{k_2})=p_{1}^{k_1}·p_{2}^{k_2}-p_{1}^{k_1-1}·p_{2}^{k_2}-p_{1}^{k_1}·p_{2}^{k_2-1}+p_{1}^{k_1-1}·p_{2}^{k_2-1} If we let k1=k1+1k_1'=k_1+1, the above formula only needs to be transformed into φ(p1k1p2k2)=p1k1+1p2k2p1k1+11p2k2p1k1+1p2k21+p1k1p2k21φ(p_{1}^{k_1'}·p_{2}^{k_2})=p_{1}^{k_1+1}·p_{2}^{k_2}-p_{1}^{k_1+1-1}·p_{2}^{k_2}-p_{1}^{k_1+1}·p_{2}^{k_2-1}+p_{1}^{k_1}·p_{2}^{k_2-1}

We can obtain φ(p1k1p2k2)=φ(p1k1p2k2)pφ(p_{1}^{k_1'}·p_{2}^{k_2})=φ(p_{1}^{k_1}·p_{2}^{k_2})·p.

This formula can also be extended.

Alright, now we can perform the linear sieve.

In the linear sieve, if we want to use a number xx and a prime pp to update the φφ of xpxp, we first check the condition pxp|x: {φ(p)=p1φ(xp)=φ(x)(p1)(px)φ(xp)=φ(x)p(px)\left\{ \begin{aligned} φ(p) &= p-1 \\ φ(xp) &= φ(x)*(p-1) & (p \nmid x) \\ φ(xp) &= φ(x)*p & (p|x) \end{aligned} \right.

Comments

0

No comments yet.