Pagini recente » Istoria paginii utilizator/ralukutza_k_2oo7 | Cod sursa (job #2007463) | Cod sursa (job #960082) | Cod sursa (job #2649061) | Cod sursa (job #112843)
Cod sursa(job #112843)
#include <stdio.h>
#include <malloc.h>
unsigned long n;
double* totient;
double calcul(int n)
{
int i,j;
double s=0;
totient[2]=1;
for(i=2; i<=n/2; i++)
{
if (totient[i]==0) totient[i]=1;
if (totient[i]==1)
{
for (j=i+i; j<=n; j+=i)
{
if (totient[j]==0) totient[j]=1;
if (totient[j]>1) totient[j]=totient[j]*(i-1)/i;
else totient[j]=j*(i-1)/i;
}
}
if (totient[i]==1) totient[i]=i-1;
s+=totient[i];
}
for (i=n; i>n/2; i--)
{
if ((totient[i]==0) || (totient[i]==1)) totient[i]=i-1;
s+=totient[i];
}
return s;
}
int main()
{
FILE *f, *g;
f=fopen("fractii.in","r");
fscanf(f,"%lu",&n);
totient=(double*) calloc ((n+1),sizeof(double));
g=fopen("fractii.out","w");
fprintf(g,"%lf",2*calcul(n)+1);
return 0;
}