Pagini recente » Cod sursa (job #1573132) | Cod sursa (job #320930) | Cod sursa (job #1037035) | Cod sursa (job #322770) | Cod sursa (job #705827)
Cod sursa(job #705827)
#include <stdio.h>
#include <cmath>
/* function which uses the Prime Sieve of Eratosthenes compute the totient function */
void computeTotient(const int n, int *tot){
int i, j;
for(i = 1;i<=n; ++i){
/*first, set the totient function as if all numbers are prime */
tot[i] = i-1;
}
for(i= 2; i<=n; ++i){
for(j = 2 * i; j<=n; j += i){
tot[j] -= tot[i];
}
}
}
long long countFractions(const int n, int *tot){
long long count = 0;
int i;
for(i=2;i<=n;++i){
count += tot[i];
}
return 1 + 2*count;
}
int main(){
freopen("fractii.in","r", stdin);
freopen("fractii.out","w", stdout);
int n;
int *tot;
scanf("%d", &n);
fclose(stdin);
tot = new int[n+1](); // allocate memory for the totient function
computeTotient(n, tot);
long long fractions = countFractions(n, tot);
printf("%lld", fractions);
delete [] tot;
fclose(stdout);
return 0;
}