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 is prime? (single query) Brute force: use numbers in to test . If there exists a in the interval such that , then is composite; otherwise, it is prime. Complexity . Unique prime factorization: Obviously, an integer can be decomposed into the product of several primes ,
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 is prime? (single query)
Brute force: use numbers in to test . If there exists a in the interval such that , then is composite; otherwise, it is prime.
Complexity
Unique prime factorization
Obviously, an integer can be decomposed into the product of several primes , i.e., the unique prime factorization of a number:
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 in ascending order with complexity . If there exists such that , let . If still , continue doing so. For the same , count the number of times it appears.
Intuitively, we can see that each obtained is necessarily a prime , and the counted number of occurrences is the exponent of this prime.
Proof: If there exists a that can be decomposed into two (or more) primes , then for any such , we must have . If so, this prime must have appeared earlier.
Complexity
How many factors? I
How to find the number of factors of a number?
Represent the primes after decomposing a number as a multiset . The product of the elements in any non-empty subset is a factor of .
Consider the number of times a certain prime factor of is used. The total number of factors is .
Using the above method for prime factorization, we can obtain each exponent.
Complexity
Linear sieve
Find in whether each number in is prime.
Using the property of "unique prime factorization", we try to make each number be updated only once:
- If a number has not been marked, this number is prime.
- For each number , we traverse the prime list once and mark .
Preprocessing , query .
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 representing the smallest prime factor of ( is some prime):
Preprocessing , query .
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 by repeatedly setting . ( represents the smallest prime factor of )
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 . It can be found that the maximum number of prime factors of is on the order of about .
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 . It can be found that this number grows very quickly. When , this number already exceeds .
Preprocessing , query .
Euler's totient function
Definition: Euler's totient function represents the number of numbers less than that are coprime to .
Multiplicativity of Euler's totient function
Definition: For , if when , , then is a multiplicative function.
As is well known, is a multiplicative function, but why is it multiplicative?
a If is prime,
The conclusion is obvious.
b If is prime,
Consider inclusion-exclusion: subtract the multiples of from the positive integers. Because there is only one prime factor , all multiples of are multiples of , and other numbers are not multiples of . There are such numbers.
c If are primes,
Similar to , we consider inclusion-exclusion. Because , the overlapping part is only multiples of . The formula is:
Using , we can separately find and .
Obviously, multiplying them equals the above formula, so it is proved.
d Euler's totient function is multiplicative
Consider extending to the case of multiple prime factors , doing similar inclusion-exclusion. Factoring the formula obtained by inclusion-exclusion gives the following formula:
How to find
Factoring out the common factor from the formula in part above gives
Then we use the method in Finding the unique prime factorization to obtain each prime factor and substitute it into the formula.
Complexity
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 and a number with ,
Consider conclusion in Multiplicativity of Euler's totient function: If we let , the above formula only needs to be transformed into
We can obtain .
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 and a prime to update the of , we first check the condition :
Comments
0No comments yet.