Pagini recente » Cod sursa (job #1480216) | Cod sursa (job #594941) | Cod sursa (job #1335467) | Cod sursa (job #2822698) | Cod sursa (job #1936315)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
static const int MAX_SIZE_FOR_NUMBER = 1000;
using namespace std;
int main() {
ifstream inputFile;
inputFile.open("fractii.in", ifstream::in);
if (!inputFile.is_open()) {
cout << "Input file is not opened";
return -1;
}
long long int maxNumber;
inputFile >> maxNumber;
inputFile.close();
long long iterator = 2;
int isPrimeStorage[maxNumber];
memset(isPrimeStorage, 0, sizeof(isPrimeStorage));
double totientStorage[maxNumber];
memset(totientStorage, 0, sizeof(totientStorage));
long long int totientTotal = 1;
while (iterator <= maxNumber) {
long long int iteratorStorageIndex = iterator - 1;
if (isPrimeStorage[iteratorStorageIndex] == 0) {
totientTotal += 2 * (iterator - 1);
long long int iteratorCopy = iterator + iterator;
double currentPrimeInverse = 1.0 / iterator;
double totientPartForPrime = 1.0 - currentPrimeInverse;
while (iteratorCopy <= maxNumber) {
long long int multipliersStorageIndex = iteratorCopy - 1;
isPrimeStorage[multipliersStorageIndex] = 1;
if (totientStorage[multipliersStorageIndex] == 0) {
totientStorage[multipliersStorageIndex] = iteratorCopy;
}
totientStorage[multipliersStorageIndex] *= totientPartForPrime;
iteratorCopy += iterator;
}
} else {
totientTotal += 2 + 2 * ((int) totientStorage[iteratorStorageIndex] - 1);
}
iterator++;
}
ofstream outputFile;
outputFile.open("fractii.out", ofstream::out);
if (!outputFile.is_open()) {
cout << "Output file is not opened";
return -1;
}
outputFile << totientTotal;
}