单个欧拉函数
int eular(int n){ int ret=1,i; for(i=2;i*i<=n;i++) if(n%i==0){ n/=i,ret*=i-1; while(n%i==0)n/=i,ret*=i; } if(n>1) ret*=n-1; return ret; }
筛法求欧拉函数
#include <cstdio> #include <iostream> using namespace std; const int maxn=3000005; long long phi[maxn]; int main(){ int i,j,a,b; for(i=1;i<=maxn;i++) phi[i]=i; for(i=2;i<=maxn;i+=2) phi[i]/=2; for(i=3;i<=maxn;i+=2)if(phi[i]==i){ for(j=i;j<=maxn;j+=i) phi[j]=phi[j]/i*(i-1); } while(scanf("%d%d",&a,&b)!=EOF){ long long ans=0; for(i=a;i<=b;i++)ans+=phi[i]; cout<<ans<<endl; } return 0; }