Pagini recente » Cod sursa (job #1226928) | Cod sursa (job #2358145) | Cod sursa (job #1601896) | Cod sursa (job #2135049) | Cod sursa (job #192058)
Cod sursa(job #192058)
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
inline unsigned long long phi(int x, int k)
{
return (x - 1) * (double)pow((double)x, (double)(k - 1));
}
unsigned long long totient(int x, bool er[])
{
int i, k;
unsigned long long rasp, aux;
if ( !er[x] ) return x - 1;
//divizibilitatea la 2
aux = (x & -x);
x /= aux;
aux >>= 1;
if (aux) rasp = aux;
else rasp = 1;
for (i = 3; x != 1; i += 2) {
if (!er[i] && !(x % i)) {
k = 1;
x /= i;
while ( !(x % i) ) { ++k; x /= i; }
rasp *= phi(i, k);
}
}
return rasp;
}
int main()
{
const int NMAX = 1000000;
bool erathos[NMAX] = {};
int n, i, j, q;
unsigned long long s;
FILE *f;
f = fopen("fractii.in", "r");
fscanf(f, "%d", &n);
fclose(f);
for (i = 4; i <= n; i += 2) erathos[i] = true;
q = sqrt(n);
for (i = 3; i <= q; i += 2)
if (!erathos[i])
for (j = i * i; j <= n; j += i) erathos[j] = true;
s = 1;
for (i = 2; i <= n; ++i)
s += totient(i, erathos) * 2;
f = fopen("fractii.out", "w");
fprintf(f, "%llu \n", s);
fclose(f);
return 0;
}