欧拉函数,n每扩大一个,就会有euler(n)个数字加入到集合中,所以欧拉函数的前n项和即为所求。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 1000006
long long phi[maxn], num[maxn];
int n;
int main()
{
//freopen("t.txt", "r", stdin);
int i, j;
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);
}
num[0] = 0;
for (int i = 1; i < maxn; i++)
num[i] = num[i - 1] + phi[i];
while (scanf("%d", &n), n != 0)
{
printf("%lld\n", num[n] - 1);
}
return 0;
}