Pagini recente » Cod sursa (job #2999949) | Cod sursa (job #1866295) | Cod sursa (job #179286) | Cod sursa (job #1867290) | Cod sursa (job #1125050)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_COUNT 1000001
FILE *in, *out;
long primes[MAX_COUNT];
long count = 0;
long long init_sum = 0;
long calculate_totient(long n)
{
long sqrt_n = n/2 + 1;
long i = 0;
long p_product = 1;
int found = 0;
while(primes[i] < sqrt_n && i<count)
{
if(n % primes[i] == 0)
{
p_product *= (primes[i] - 1);
n = n / primes[i];
found = 1;
}
i++;
}
if(!found) // the number is prime
{
primes[count++] = n;
return n-1;
}
return (long)(p_product * n );
}
long calculate_fractions(long n)
{
long index;
// init first with max count of divisors
if(n == 1)
return 1;
for(index = 2; index<=n;index++)
primes[index]= index-1;
for(index = 2; index<=n; index++)
{
long mult_index;
init_sum += primes[index];
for(mult_index=index<<1;mult_index<=n;mult_index+=index)
primes[mult_index]-=primes[index]; // decrease the number of divisors according
}
return (init_sum<<1)+1;
}
int main()
{
long result,n ;
in = fopen("fractii.in","r");
fscanf(in,"%ld",&n);
fclose(in);
result = calculate_fractions(n);
out = fopen("fractii.out","w");
fprintf(out,"%ld",result);
fclose(out);
return 0;
}