-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid0074.c
73 lines (53 loc) · 1.33 KB
/
id0074.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Licensed under the MIT License.
// Digit Factorial Chains
#include "../lib/euler.h"
#include "../lib/factorial.h"
#include "../lib/list.h"
int main(void)
{
struct List terms;
clock_t start = clock();
int* visited = calloc(1000000l, sizeof * visited);
euler_assert(visited);
long long* factorials = factorial_range(10);
euler_assert(factorials);
euler_ok(list(&terms, sizeof(long), 60));
int count = 0;
for (long m = 0; m < 1000000l; m++)
{
list_clear(&terms);
visited[m] = 1;
for (long n = m; ; )
{
euler_ok(list_add(&terms, &n));
long sum = 0;
for (long k = n; k; k /= 10)
{
sum += factorials[k % 10];
}
if (sum >= 1000000l)
{
break;
}
if (list_contains(&terms, &sum, long_equality_comparer))
{
break;
}
if (visited[sum])
{
visited[m] += visited[sum];
break;
}
visited[m]++;
n = sum;
}
if (visited[m] == 60)
{
count++;
}
}
free(visited);
free(factorials);
finalize_list(&terms);
return euler_submit(74, count, start);
}