-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid0073.c
55 lines (39 loc) · 963 Bytes
/
id0073.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Licensed under the MIT License.
// Counting Fractions in a Range
#include "../lib/euler.h"
long math_farey_count(int maxD, int n, int d)
{
int* table = malloc(maxD * sizeof * table);
if (!table)
{
return -1;
}
for (int i = 0; i < maxD; i++)
{
table[i] = i * n / d;
}
for (int i = 1; i < maxD; i++)
{
for (int j = i + i; j < maxD; j += i)
{
table[j] -= table[i];
}
}
long result = 0;
for (int i = 0; i < maxD; i++)
{
result += table[i];
}
free(table);
return result;
}
int main(void)
{
clock_t start = clock();
long lessThanOneHalf = math_farey_count(12001, 1, 2);
euler_assert(lessThanOneHalf != -1);
long lessThanOneThird = math_farey_count(12001, 1, 3);
euler_assert(lessThanOneThird != -1);
long count = lessThanOneHalf - lessThanOneThird - 1;
return euler_submit(73, count, start);
}