Euler's Totient Function

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include<bits/stdc++.

h>

using namespace std;

#define FastRead ios_base::sync_with_stdio(0);cin.tie(0);

#define endl "\n"

#define pi acos(-1)

#define NUM 1000010

#define sq(x) ((x)*(x))

typedef long long ll;

typedef long long unsigned llu;

bool mark[NUM];

vector <int> v;

void sieve()

mark[1]=true;

for(ll i=4; i<=NUM; i+=2)

mark[i]=true;

v.push_back(2);

for(ll i=3; i*i<=NUM; i+=2)

if(!mark[i])

for(ll j=i*i; j<=NUM; j+=2*i)

mark[j]=true;

}
for(ll i=1; i<=NUM; i++)

if(!mark[i])

v.push_back(i);

return;

long long phi(ll n)

ll res,val,i;

res = n;

val=sqrt(n)+1;

for(i=0; v[i]<val; i++)

if(n%v[i]==0)

while(n%v[i]==0)

n/=v[i];

res-=(res/v[i]);

if(n>1)

res-=(res/n);

return res;
}

int main()

FastRead

sieve();

ll n;

cin>>n;

cout<<phi(n);

return 0;

You might also like