-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid0072.c
56 lines (41 loc) · 864 Bytes
/
id0072.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
56
// Licensed under the MIT License.
// Counting Fractions
#include "../lib/euler.h"
long long math_summatory_totient(long max)
{
long* phi = malloc(max * sizeof * phi);
if (!phi)
{
return -1;
}
for (long m = 0; m < max; m++)
{
phi[m] = m;
}
for (long m = 2; m < max; m++)
{
if (phi[m] == m)
{
phi[m] = m - 1;
for (long n = m + m; n < max; n += m)
{
phi[n] -= phi[n] / m;
}
}
}
long long result = 0;
for (long m = 0; m < max; m++)
{
result += phi[m];
}
free(phi);
return result;
}
int main(void)
{
clock_t start = clock();
long long result = math_summatory_totient(1000001);
euler_assert(result != -1);
result--;
return euler_submit(72, result, start);
}