Pagini recente » Cod sursa (job #1166431) | Cod sursa (job #1802967) | Cod sursa (job #458442) | Cod sursa (job #269151) | Cod sursa (job #610220)
Cod sursa(job #610220)
/*
* 002-Fractii
* 2011-08-25
*
* We apply the formula n = sum(d divides n, phi(d))
* From here we have that phi(n) = n - sum(d divides n && d != n, phi(d));
*
* It's a tad slower than the other method for computing phi, the one with products, but uses less memory since it doesn't actually compute prime numbers (you'll notice it doesn't use Eratosthenes' xieve).
*/
#include <stdio.h>
#define MN (1000001)
int main()
{
int N;
int phi[MN]; // Euler's totient function
int i, j;
long long S = 0;
freopen("fractii.in", "r", stdin);
freopen("fractii.out", "w", stdout);
scanf("%d", &N);
for(i = 1; i <= N; ++i)
phi[i] = i-1;
for(i = 2; i <= N; ++i) {
for(j = i+i; j <= N; j += i)
phi[j] -= phi[i];
}
for(i = 2; i <= N; ++i)
S += phi[i];
S = 2*S+1;
printf("%lld\n", S);
return 0;
}