Returns the prime number of the linear time sieve, returns the Euler's function value of the first N, and the approximate number of the first n.

Source: Internet
Author: User

From http://www.cnblogs.com/suno/archive/2008/02/04/1064368.html

Use Product function optimization.

This article mainly introduces 3 Algorithms

1Prime Number of linear time sieve

2Returns the Euler's number of the first N in linear time.

3Evaluate the approximate number of the first N in linear time

 

I,First, we will introduce the product functions.

 

The following are wiki entries:

 

In the field of Non-number theory, a product function refers to a function with properties F (AB) = f (a) F (B) for any A and B.

 

The product function in number theory. For an arithmetic function f (n) of positive integer N ),Where F (1) = 1 and when A and B are mutually qualitative, F (AB) = f (a) F (B) is called as a product function in number theory.

If an arithmetic function f (n) conforms to F (1) = 1, and even if A and B are not of mutual quality, F (AB) = f (a) F (B ), it is fully product.

 

Example

Number of positive integers in the interaction with N using the (N)-Euler's Function

μ (N)-Mobius function, number of quality factors about the number of non-distinct numbers

Gcd (n, k)-the most common factor, when K is fixed

Number of positive factors in D (N)-N

Sum of all positive factors of σ (N)-N

σ k (n): The sum of the K power of all positive factors of a factor function, where K can be any plural. In special cases:

σ 0 (n) = D (N) and

σ 1 (n) = σ (N)

1 (n)-constant function, defined as 1 (n) = 1 (full product)

ID (N)-unit function, defined as ID (n) = N (full product)

IDK (N)-power function. For any complex number or real number K, it is defined as IDK (n) = NK (full product)

Id0 (n) = 1 (n) and

Id1 (n) = ID (N)

ε (N)-is defined as: If n = 1, ε (n) = 1; if n> 1, ε (n) = 0. It is sometimes called "Multiplication unit for Dirichlet maneuver" (full product)

(N/P)-lepete symbol, P is a fixed prime number (full product)

λ (N)-Liu weier function, number of quality factors that can divide n

Gamma (N), defined as gamma (n) = (-1) ω (N). Here, the addition function ω (n) is the number of classes with different divisible N values.

All Dirichlet features are fully Product

 

 

Ii. Introduce the linear screening Prime Number Method

 

Bool notp [Mr]; // prime number determination

_ Int64 PR [670000], Pn, ans; // PR stores prime numbers, and the current number of prime numbers of Pn.

 

Void getprime ()

{

Pn = 0;

Memset (notp, 0, sizeof (notp ));

For (INT I = 2; I <Mr; I ++)

{

If (! Notp [I]) PR [pn ++] = I;

For (Int J = 0; j <PN & pr [J] * I <Mr; j ++)

{

Notp [pr [J] * I] = 1;

If (I % PR [J] = 0) break;

}

}

}

 

UsedEach sum must have a minimum prime factor..

Total numberIt is screened exactly once by its smallest prime factor.. So it is linear time.

Code:

If (I % PR [J] = 0) break;

The prime number in the PR array increases progressively. When I can divide the total number of PR [J], the sum of I * PR [J + 1] is certainly filtered out by a certain number multiplied by PR [J.

Because I contains PR [J], PR [J] is smaller than PR [J + 1. The next prime number is the same. So you don't need to screen it down.

Before meeting the I % PR [J] = 0 condition and when the first condition is met, PR [J] must be the minimum factor of PR [J] * I.

 

 

3. Optimization Algorithm Based on Linear screening prime number Algorithm

Based on this linear screening prime number algorithm, we can easily obtain the minimum prime factor of a certain number.

Because when I % PR [J]! When the value is 0, the least prime factor PR [J] interacts with I. f (I * PR [J]) = f (I) can be obtained directly if the product function is satisfied) * F (PR [J]).

However, when I % PR [J] = 0, we must calculate based on the features of the product function. or save and recursive additional information while screening. in short, we need O (1) to obtain f (I * PR [J]) and complete recursive additional information.

 

The two examples below are the Euler's function PHI and the number of approx. These two are the most common and most useful for optimization.

Using the above properties, we can easily roll out the first N with O (n) time.

Of course, other product functions can be optimized by using this property. Here we only introduce two common and valuable optimization functions.

 

1)Euler's function (PHI)

Traditional algorithms:

For a prime number P and p | n (n can divide P)

If (N/P) % I = 0) PHI (n) = PHI (N/P) * I;

Else PHI (n) = PHI (N/P) * (I-1 );

 

This traditional algorithm is useful in screening prime number algorithms.

P is the minimum prime factor of n. When N/P contains this factor p, Phi (n) = PHI (N/P) * I; otherwise PHI (N) = PHI (N/P) * (I-1 );

P is PR [J], N/P is I, n is I * PR [J.

 

 

2)Divnum)

The approximate number cannot be as natural as Phi, but there are still good methods.

Personalized features

Divnum (n) = (E1 + 1) * (E2 + 1)... (EI indicates the number of I prime factor of n .)

The traditional method is to break down the prime factor for each number and obtain the number of each factor and use the above formula.

 

Open a space E [I] to indicate the number of times of least prime factor

Let's talk about the following:

Sifted to the J-th prime number of I

 

For divnum

If I | PR [J], divnum [I * PR [J] = divsum [I]/(E [I] + 1) * (E [I] + 2) // Add 1 to the minimum prime factor count

Otherwise, divnum [I * PR [J] = divnum [I] * divnum [pr [J] // satisfies the product function conditions.

 

For E

If I | PR [J] E [I * PR [J] = E [I] + 1; // The number of least prime factors is increased by 1

Otherwise, E [I * PR [J] = 1; // PR [J] Is once.

 

The Eular Function Code is as follows:

# Include <cstdlib> # include <cstdio> # include <algorithm> # define maxn 1000000 using namespace STD; int P [maxn + 5], PRI [1000000], idx =-1; int Phi [maxn + 5]; void getprime () {for (INT I = 2; I <= maxn; ++ I) {If (! P [I]) {// indicates that I is a prime number pri [++ idx] = I;} For (Int J = 0; j <= idx & pri [J] * I <= maxn; ++ J) {// traverse all prime factor p [pri [J] * I] = 1; if (I % pri [J] = 0) {// If I Can divisible pri [J], I * Pri [J + 1] will be divisible by the number of PRI [J] Break ;}}} printf ("idx = % d \ n", idx); For (INT I = 0; I <= 100; ++ I) {printf ("% d ", PRI [I]) ;}puts ("") ;}// calculate the value of Phi [n = P ^ e]. Since P is a prime number, we can know that ~ The numbers in P ^ e are 1 * P, 2 * P, 3 * p... [P ^ (E-1)] * P // a total of P ^ (E-1) not with P ^ e mutual quality, so Phi [N] = P ^ e-P ^ (E-1) = [P ^ (E-1)] * (p-1) = <Phi [N/P] * P> // n = p1 ^ E1 * P2 ^ e2 *.... if e1 = 1, then the product of the Euler's function is directly equal to Phi [I/P1] * Phi [P1] // If E1! = 1, then we can use the formula in the brackets above to obtain Phi [N] = Phi [I/P1] * p1void Eular () {for (INT I = 2; I <= maxn; ++ I) {If (! P [I]) {Phi [I] = I-1; // if it is a prime number, the Euler's function is the I-1 continue;} For (Int J = 0; PRI [J] * Pri [J] <= I; ++ J) {// the incoming I must be a sum of if (I % pri [J] = 0) {// If pri [J] is a prime factor of I if (I/pri [J] % pri [J] = 0) {// and the prime factor has more than two indexes Phi [I] = pri [J] * Phi [I/pri [J]; // Multiply this prime factor} else {Phi [I] = Phi [pri [J] * Phi [I/pri [J];} break ;}} printf ("Phi [% d] = % d \ n", I, Phi [I]); getchar () ;}return ;} int main () {getprime (); eular (); // system ("pause"); Return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.