Cod sursa(job #2924174)

Utilizator monsieurlazarLazar Dragos George monsieurlazar Data 26 septembrie 2022 18:34:17
Problema Fractii Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
using namespace std;
ifstream fin("fractii.in");
ofstream fout("fractii.out");
int n;
long long count;
// https://www.geeksforgeeks.org/eulers-totient-function/
int fphi(int n) {
  // Initialize result as n
  float result = n;
  // Consider all prime factors of n
  // and for every prime factor p,
  // multiply result with (1 - 1/p)
  for (int p = 2; p * p <= n; ++p) {
    // Check if p is a prime factor.
    if (n % p == 0) {
      // If yes, then update n and result
      while (n % p == 0) {
        n /= p;
      }
      result *= (1.0 - (1.0 / (float)p));
    }
  }
  // If n has a prime factor greater than sqrt(n)
  // (There can be at-most one such prime factor)
  if (n > 1) {
    result *= (1.0 - (1.0 / (float)n));
  }
  return (int)result;
}

int main() {
  fin >> n;
  count++;
  for (int i = 2; i <= n; i++) {
    count += 2 * fphi(i);
    // fout<<phi(i)<<endl;
  }
  fout << count;
  fin.close();
  fout.close();
  return 0;
}
// 11 -> 83
// 15 -> 143
// 31 -> 615
// 99 -> 6007
// 100 -> 6087
// 1000000 -> 607927104783