Pagini recente » Cod sursa (job #1460045) | Cod sursa (job #2069643) | Cod sursa (job #2922302) | Cod sursa (job #162476) | Cod sursa (job #200413)
Cod sursa(job #200413)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
unsigned int * eratosthenesSieve (unsigned int n) {
unsigned int * sieve = new unsigned int[n+1];
memset (sieve, 1, (n+1) * sizeof(unsigned int));
sieve[1] = 0;
sieve[0] = 0;
for (unsigned int i = 2; i*i <= n; i += 1) {
if (sieve[i]) {
unsigned int j = 2;
while (i*j <= n) {
sieve[i*j] = 0;
j++;
}
}
}
return sieve;
}
unsigned int * generateTotients (unsigned int n)
{
unsigned int * sieve = eratosthenesSieve (n);
unsigned int * totient = new unsigned int[n+1];
memset (totient, 0, (n+1) * sizeof (unsigned int));
for (unsigned int i = 2; i <= n; i++)
{
if (totient[i])
continue;
if (sieve[i]) {
totient[i] = i-1;
unsigned int result = i;
while ((result *= i) <= n)
{
totient[result] = result/i * (i-1);
}
}
else {
//cout << "i = " << i << endl;
totient[i] = i;
double product = 1.0;
for (unsigned int j = 2; j < i; j++)
{
//cout << "\tj = " << j << " ";
if (sieve[j] && i%j == 0) {
product *= (1.0-(1.0/(double)j));
//cout << "\t\tproduct = " << product;
}
//cout << endl;
}
totient[i] = (unsigned int)ceil((double)totient[i] * product);
unsigned int result = i;
while ((result *= i) <= n)
{
totient[result] = result/i * totient[i];
}
}
}
delete[] sieve;
return totient;
}
bool isPrime (unsigned int n)
{
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}
int main()
{
unsigned int n;
ifstream fin ("fractii.in");
cin >> n;
fin.close();
unsigned int * totient = generateTotients (n);
long total = 1;
for (unsigned int i = 2; i <= n; i++)
{
total += totient [i] * 2;
//cout << "tot(" << i << ")=" << totient[i] << endl;
}
ofstream fout ("fractii.out");
fout << total;
cout << total;
fout.close();
delete[] totient;
return 0;
}