-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid0032.c
77 lines (61 loc) · 1.71 KB
/
id0032.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
// Licensed under the MIT License.
// Pandigital Products
#include "../lib/euler.h"
#include "../lib/list.h"
#include "../lib/permutation_iterator.h"
int main(void)
{
struct List set;
struct PermutationIterator it;
int digits[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
clock_t start = clock();
euler_ok(list(&set, sizeof(long), 0));
for (permutation_begin(&it, digits, 9, sizeof * digits, int_comparer);
!it.end;
permutation_next(&it))
{
for (int i = 1; i < 7; i++)
{
for (int j = i + 1; j < 8; j++)
{
long a = 0;
long b = 0;
long c = 0;
for (int k = 0; k < i; k++)
{
a = 10 * a + digits[k];
}
for (int k = i; k < j; k++)
{
b = 10 * b + digits[k];
}
for (int k = j; k < 9; k++)
{
c = 10 * c + digits[k];
}
long product = a * b;
if (product > c)
{
break;
}
if (product != c)
{
continue;
}
if (!list_contains(&set, &product, long_equality_comparer))
{
euler_ok(list_add(&set, &product));
}
}
}
}
long long sum = 0;
long* setBegin = set.items;
long* setEnd = setBegin + set.count;
for (long* it = setBegin; it < setEnd; it++)
{
sum += *it;
}
finalize_list(&set);
return euler_submit(32, sum, start);
}