Euler's linear sieve method for prime number (the way to achieve the value of Euler function) __ Euler sieve method

Source: Internet
Author: User

Let's take a look at the most classic Eratsteni sieve method. Time Complexity of O (n loglog N)

int ANS[MAXN];
void Prime (int n)
{
	int cnt=0;
	memset (prime,1,sizeof (Prime));
	prime[0]=prime[1]=0;
	for (int i=2;i<n;i++)
	{
		if (Vis[i])
		{
		   ans[cnt++]=i;//save prime for 
		   (int j=i*i;j<n;j+=i)//i* I began with a slightly optimized
		   prime[j]=0;//not prime 
		} return
	;

Obviously, when a number is a prime, then his multiples must be composite numbers, the filter tag can be. From the i*i and not from the i*2, because the i*3,i*2 has already been screened by 2, 3.

As a result, we can also find that some of the composite numbers are screened repeatedly, such as 30,2*15 sieve once, 5*6 repeat sieve, so we have the following Euler linear sieve method.

No repetition of the sieve is the complexity of the linear O (n).

const int maxn=3000001;
int prime[maxn];//Save prime number 
bool vis[maxn];//initialize 
int prime (int n)
{
	int cnt=0;
	memset (Vis) (vis,0,sizeof);
	for (int i=2;i<n;i++)
	{
		if (!vis[i])
		prime[cnt++]=i;
		for (int j=0;j<cnt&&i*prime[j]<n;j++)
		{
			vis[i*prime[j]]=1;
			if (i%prime[j]==0)//Key break 
			;
		}
	}
	Return cnt;//returns the number of prime numbers less than n 
}
First, a condition is defined, and any composite can be expressed as a product of a series of primes.
Each composite is then used to have a minimum element factor, and each composite is screened just once by its minimum element factor. So for linear time.
The code is reflected in the following:
if (i%prime[j]==0) break;
The primes in the prime array are incremented, and when I can divide prime[j], then i*prime[j+1] This composite must be prime[j multiplied by some number.
Because I contain prime[j], Prime[j] is smaller than prime[j+1]. The next primes are the same. So you don't have to sift it down.
Before satisfying the condition of i%prme[j]==0 and first satisfying the condition, prime[j] must be the minimum factor of prime[j]*i.

If you don't understand it, you can simulate it manually.

A simple example of a direct application. To find the number of the element within N.

http://blog.csdn.net/nk_test/article/details/46242311

Euler function: In number theory, for positive integer n, Euler function is less than or equal to n coprime number of numbers with N.

First, give a conclusion:

Set p to be prime,

If P is an approximate divisor of x, then E (x*p) =e (x) *p.

If P is not an approximate divisor of x, then E (x*p) =e (x) *e (p) =e (x) * (p-1).

Proved as follows:

E (x) represents the number of positive integers that are smaller than x and coprime with X.
* If P is prime, E (p) =p-1.
*e (p^k) =p^k-p^ (k-1) = (p-1) *p^ (k-1)
Certificate: Make N=p^k, the number of positive integers less than n is a total of n-1 (p^k-1), with a number of P-quality [p^ (K-1)-1] (respectively 1*p,2*p,3*p ... p (p^ (K-1)-1)).
therefore e (p^k) = (p^k-1)-(p^ (k-1)-1) =p^k-p^ (k-1). Certificate.
* If AB coprime, then E (a*b) =e (a) *e (b), Euler functions are integrable functions. The
* can be uniquely decomposed into N=p1^a1*p2^a2*p3^a3*...*pn^an (pi is prime) for any number n.
then E (n) =e (P1^A1) *e (P2^A2) *e (P3^A3) *...*e (pn^an)     
      = (p1-1) *p1^ (a1-1) * ( p2-1) *p2^ (a2-1) *...* (pn-1) *pn^ (an-1)
      = (P1^a1*p2^a2*p3^a3*...*pn^an) *[(p1-1) * (p2-1) * (p3-1) *...* (pn-1)]/(P1*P2*P3*...*PN)
      =n* (1-1/P1) * (1-1/P2) *...* (1-1/PN)
* E (p^k)    = ( p-1) *p^ (k-1) = (p-1) *p^ (k-2) *p
  E (p^ (k-1)) = (p-1) *p^ (k-2)
-> when k>1, E (p^k) =e (p*p^ (k-1)) =e (p^ (k-1)) *p.
  (When K=1, E (P) =p-1.)
by Upper: set P to be prime,
  if p is an approximate divisor of x, then E (x*p) =e (x) *p.
  If p is not an approximate divisor of x, E (x*p) =e (x) *e (p) =e (x) * (p-1).   Proof ends.

Specific application of http://acm.hdu.edu.cn/showproblem.php?pid=2824

The Euler function of the interval is obtained.

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int maxn=3000001;
int prime[maxn];//Save prime number 
bool vis[maxn];//initialize 
int phi[maxn];//Euler function 
void prime (int n)
{
	int cnt=0;
	memset (Vis) (vis,0,sizeof);
	for (int i=2;i<n;i++)
	{
		if (!vis[i])
		{
			prime[cnt++]=i;
			phi[i]=i-1;//if P is prime,then phi[i]=i-1
		} for
		(int j=0;j<cnt&&i*prime[j]<n;j++)
		{
			__int64 K=i*prime[j];
			Vis[k]=1;
			if (i%prime[j]==0)//Key 
			{
				phi[k]=phi[i]*prime[j];
				break;
			else
			phi[k]=phi[i]* (prime[j]-1);
		
		}
	}
int main ()
{
	int a,b;
	Prime (3000000);
	while (cin>>a>>b)
	{
		__int64 ans=0;
		for (int i=a;i<=b;i++)
		ans+=phi[i];
		cout<<ans<<endl;
	}
}

and http://acm.hdu.edu.cn/showproblem.php?pid=3501.

Analysis: For integer n, if X (X<n) and N coprime, then (N-x) is also coprime with N; if X (X<n) and n are not coprime, then (n-x) also with N is not coprime. Once you know this, you can conclude that the number of X and N coprime in 0<x<n is assumed to be num (which can be obtained by Euler function), then all x and SUM=NUM*N/2 with n coprime.

/* Using the Euler function can be solved, 1~n than n small and with the number of N-1~n-1 sum is
sum (n) = n * PHI (n)/2; Then you can find the sum of the, and then
subtract sum (n). *
#include <iostream>
#include <cstdio>
#include <cstring>
#include < algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
#define MOD 1000000007
LL N;
ll Eular (ll N) {
    ll cnt=1;
    for (int i=2; i*i<=n; i++) {
        if (n%i==0) {
            cnt*= (i-1);
            n/=i;
            while (n%i==0) {
                n/=i;
                Cnt*=i
    }
    }} if (n>1) cnt*= (n-1);
    return cnt;
}

int main () {while
    (~scanf ("%lld", &n) &&n) {
        LL ans= (n+1) *n/2-n;
        Ans-=eular (n) *n/2;
        printf ("%i64d\n", (ans%mod+mod)%mod);
    }
    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.