Cod sursa(job #213869)

Utilizator philandrewChindea Filip philandrew Data 11 octombrie 2008 21:17:06
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>

int prime[1000];
int np;
  
using namespace std;

int eulerphi (int nr) {
  int prod = 1;
  int *pt = &prime[1];
  int *pf = &prime[np];
  int ds = sizeof(int);
  while (pt <= pf) {
    if (nr%(*pt) == 0){
      while (nr%(*pt) == 0){
        prod *= *pt;
        nr /= *pt;
      }
      prod /= *pt;
      prod *= *pt - 1;
      if (nr == 1) pt = pf;
    }
    pt += ds;
  }
  if (nr > 1) prod *= nr - 1;
  return prod;
}

bool eprim(int i){
  for (int j = 2; j*j<=i; j++){
    if (i%j == 0){
      return false;
    }
  }
  return true;
}

void genprimes(){
  int i;
  np = 0;
  for (i = 2; i <= 1000; i++){
    if (eprim(i)){
      np++;
      prime[np] = i;
    }
  }
}

int main(){
  int n;
  long long nr;
  ifstream fin("fractii.in");
  ofstream fout("fractii.out");
  fin >> n;
  genprimes();
  
  nr = -1;
  for (int i = 1; i <= n; i++) nr += 2*eulerphi(i);
  
  fout << nr;
  fin.close();
  fout.close();
  return 0x0;
}