Pagini recente » Cod sursa (job #599839) | Cod sursa (job #1941587) | Cod sursa (job #1012341) | Cod sursa (job #1252081) | Cod sursa (job #634347)
Cod sursa(job #634347)
// Fractii.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
int cmmdc(int a, int b)
{
int r;
r = a%b;
while (r != 0)
{
a = b;
b = r;
r = a % b;
}
return b;
}
int main(int argc, char* argv[])
{
int n;
int i, j;
FILE *f1, *f2;
double l;
long long sum = 0;
double *a;
char *sel;
f1 = fopen("fractii.in", "r");
f2 = fopen("fractii.out", "w");
if (f1 == NULL || f2 == NULL)
{
return 0;
}
if (fscanf(f1, "%d", &n) <= 0)
{
fclose(f1);
return 0;
}
a = (double *) malloc(sizeof(double) * n);
sel = (char *) malloc(sizeof(int) * n);
for (i = 0; i < n; i++)
{
a[i] = i + 1;
sel[i] = 0;
}
for (i = 1; i < n; i++)
{
if (sel[i] == 0)
{
l = 1 - (double) 1 / (i+1);
a[i] *= l;
for (j = 2*(i+1); j <= n; j += (i+1))
{
a[j-1] *= l;
sel[j-1] = 1;
}
}
}
for (i = 0; i < n; i++)
{
sum += a[i] * 2;
}
sum = sum - 1; // 1/1 era pus de 2 ori
fprintf(f2, "%lld", sum);
return 0;
}