Pagini recente » Cod sursa (job #244182) | Cod sursa (job #2767040) | Cod sursa (job #2788309) | Cod sursa (job #1689443) | Cod sursa (job #2927122)
#include <fstream>
void ComputeTotientFunction(int phi[], int n) {
for (int i = 1; i <= n; i++) { // initialisation
phi[i] = i;
}
phi[1] = 0;
for (int i = 2; i <= n; i++) {
if (phi[i] == i) {
phi[i]--; // decrement the value so that it is correct for prime numbers
for (int j = 2; j * i <= n; j++) { // go through all the multiples of i
phi[j * i] = phi[j * i] / i * (i - 1); // update their value
}
}
}
}
int main() {
std::ifstream fin("fractii.in");
std::ofstream fout("fractii.out");
int n;
fin >> n;
int phi[n + 1]; // Euler's totient function, phi[i] = # of numbers < i that are relatively prime to i
ComputeTotientFunction(phi, n);
int result = 0;
for (int i = 1; i <= n; i++) {
result += phi[i] * 2; // fractions can be reversed, so multiply by 2
}
fout << result + 1; // add the fraction 1 / 1
return 0;
}