-
Notifications
You must be signed in to change notification settings - Fork 0
/
id0061.c
127 lines (102 loc) · 2.87 KB
/
id0061.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Licensed under the MIT License.
// Cyclic Figurate Numbers
#include "../lib/euler.h"
#include "../lib/list.h"
static bool math_is_cyclic(int a, int b)
{
return a != b && a / 100 == b % 100;
}
static bool math_is_octagonal_or_smaller_polygonal(int x)
{
for (int s = 3; s < 6; s++)
{
if (math_is_polygonal(s, x, NULL))
{
return true;
}
}
return math_is_polygonal(7, x, NULL) || math_is_polygonal(8, x, NULL);
}
static bool math_is_permuted_polygonal(int values[])
{
int flags = 0;
for (int* x = values; x < values + 6; x++)
{
for (int s = 8; s >= 3; s--)
{
if (math_is_polygonal(s, *x, NULL))
{
flags |= (1 << s);
break;
}
}
}
return flags == 0x1f8;
}
static int math_cyclic_polygonal_sum(List polygonals)
{
int* begin = polygonals->items;
int* end = begin + polygonals->count;
for (int* a = begin; a < end; a++)
{
for (int* b = begin; b < end; b++)
{
if (!math_is_cyclic(*a, *b))
{
continue;
}
for (int* c = begin; c < end; c++)
{
if (!math_is_cyclic(*b, *c))
{
continue;
}
for (int* d = begin; d < end; d++)
{
if (!math_is_cyclic(*c, *d))
{
continue;
}
for (int* e = begin; e < end; e++)
{
if (!math_is_cyclic(*d, *e))
{
continue;
}
for (int* f = begin; f < end; f++)
{
if (!math_is_cyclic(*e, *f) ||
!math_is_cyclic(*f, *a))
{
continue;
}
int values[] = { *a, *b, *c, *d, *e, *f };
if (!math_is_permuted_polygonal(values))
{
continue;
}
return *a + *b + *c + *d + *e + *f;
}
}
}
}
}
}
return -1;
}
int main(void)
{
struct List polygonals;
clock_t start = clock();
euler_ok(list(&polygonals, sizeof(int), 0));
for (int x = 1000; x < 10000; x++)
{
if (math_is_octagonal_or_smaller_polygonal(x))
{
euler_ok(list_add(&polygonals, &x));
}
}
int sum = math_cyclic_polygonal_sum(&polygonals);
finalize_list(&polygonals);
return euler_submit(61, sum, start);
}