You are on page 1of 1

//change according to n

ll int phi[1001];
void totient(int n)
{
for(int i=1;i<=n;i++)
{
//not evaluated state, since max possible is (i-1)
phi[i]=i;
}
for(int p=2;p<=n;p++)
{
//if not computed, it is a prime
if(phi[p]==p)
{
phi[p]=p-1;
for(int i=2*p;i<=n;i+=p)
{
//add contribution of p to its multiple
phi[i]=(phi[i]/p)*(p-1);
}
}
}
}

You might also like