Pagini recente » Cod sursa (job #2670922) | Cod sursa (job #1020078) | Cod sursa (job #967270) | Cod sursa (job #286012) | Cod sursa (job #1342311)
#include <stdio.h>
#include <string.h>
#define MAX_FILE_NAME 128
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
unsigned long long int get_number_of_fractions(int n);
int main()
{
char *in_file_name = "fractii.in";
char *out_file_name = "fractii.out";
FILE *data_file, *out_file;
int n, read_status;
unsigned long long int total;
if ((data_file = fopen(in_file_name, "r")) == NULL) {
printf("Sorry, can't open the input file.\n");
return EXIT_FAILURE;
}
else if ((out_file = fopen(out_file_name, "w")) == NULL) {
printf("Sorry, can't create the output file.\n");
fclose(data_file);
return EXIT_FAILURE;
}
while (!feof(data_file)) {
read_status = fscanf(data_file, "%d", &n);
if (1 == read_status) {
total = get_number_of_fractions(n);
fprintf(out_file, "%llu\n", total);
}
}
fclose(out_file);
fclose(data_file);
return EXIT_SUCCESS;
}
unsigned long long int get_number_of_fractions(int n)
{
int numerator, denominator;
unsigned long long int total_fractions = 0ULL;
int gcd(int u, int v);
for (numerator = 1; numerator <= n; ++numerator) {
for (denominator = 1; denominator <= n; ++denominator) {
if (!(denominator % numerator) && numerator != 1) {
continue;
}
else {
if (gcd(numerator, denominator) == 1)
++total_fractions;
}
}
}
return total_fractions;
}
int gcd(int u, int v)
{
int temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
return u;
}