Euler's Function + prime screening, Euler's prime screening

Source: Internet
Author: User

Euler's Function + prime screening, Euler's prime screening
The Euler's function is a formula found by Euler about prime numbers. Then we compile a function to implement this formula.

Euler finds that this formula can be used to calculate the number of numbers in the positive integers less than or equal to n and the number of n mutex:

Euler (x) = x (1-1/p1) (1-1/p2) (1-1/p3) (1-1/p4 )... (1-1/pn), where p1, p2 ...... All prime factors whose pn is x, and whose x is not an integer of 0. Euler (1) = 1 (the number of unique and 1 mutual quality is 1 itself ).

Extension of euler's formula: The sum of all quality factors of a number is euler (n) * n/2.

In fact, you can simply read the template and add annotations.

The filtering principle is to find the factor n and remove the number of factors containing n, that is, to remove the numbers that are not in mutual quality with n.

Since it is the number of mutual quality with n, we can directly filter it, see the template:

Int phi (int n)

{Int res = n;/if the number of existing n is different from that of n, filter and remove the existing n.

For (I = 2; I * I <= n; I ++)

{If (n % I = 0)/if this number is a factor of n, minus the number of the numbers below n containing this factor, assume n = 8, if the value is less than or equal to 8/2, there are = 4 Public factors.

{Res-= res/I;

While (n % I = 0)/Division n continuously

N = n/I;

}

}

If (n> 1)/if n is greater than 1, then n is also a factor other than 1.

Res-= res/n;

Return res;

}

Sometimes multiple numbers of Euler's values are used. Therefore, you need to obtain the Euler's values for the numbers 1 to n, that is, to create a table.

Calculate and store the Euler's values from 1 to n to an array, evaluate method, and code:

Void phi (int n) can be understood above. The number of requests below is similar.

{For (int I = 1; I <= n; I ++)

P [I] = I; Original Value

For (int I = 2; I <= n; I ++)

If (p [I] = I)

{For (int j = I; j <= n; j + = I) Filtering

P [j] = p [j]-p [j]/I;

}

}

Prime Number Screening: it allows you to determine whether any number is a prime number. If you ask one, it will obviously time out. Therefore, you need to first obtain all the prime numbers and use the 'distinct' method to find them, this is called prime screening.

The principle is that if a number has a factor other than 1 and itself, it will mark it as not a prime number, and the last unlabeled is a prime number.

Directly look at the code and add Annotations:

# Include <iostream>

# Include <cstring>

# Deprecision MAX 1000001

Int flag [MAX];

Int main ()

{Memset (flag, 0, sizeof (flag ));

Flag [1] = 1;/1 indicates that it is not a prime number, and 0 indicates a prime number.

For (int I = 4; I <MAX; I + = 2)

Flag [I] = 1;/first mark the even number as not

For (int I = 3; I * I <MAX; I + = 2)

For (int j = I * I; j <MAX; j + = I)/the multiple mark of the odd number is not

Flag [j] = 1;

Int n;

While (cin> n)

{If (flag [n] = 0)

Cout <"YES" <endl;

Else

Cout <"NO" <endl;

}

}

Prime screening is often used to let you judge a large number of prime numbers, or to find a large number of prime numbers. Of course, if the number is small, it would be good to judge by regular.

 

To be continued ......

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.