-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem039.cpp
More file actions
92 lines (79 loc) · 2.36 KB
/
problem039.cpp
File metadata and controls
92 lines (79 loc) · 2.36 KB
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
// Compile with the following command:
// g++ problem039.cpp -lgsl -lgslcblas
#include <gsl/gsl_blas.h>
#include <iostream>
#include <vector>
const double m1[] = { -1, 2, 2,
-2, 1, 2,
-2, 2, 3 };
const double m2[] = { 1, 2, 2,
2, 1, 2,
2, 2, 3 };
const double m3[] = { 1, -2, 2,
2, -1, 2,
2, -2, 3 };
gsl_matrix_const_view a[] = { gsl_matrix_const_view_array(m1, 3, 3),
gsl_matrix_const_view_array(m2, 3, 3),
gsl_matrix_const_view_array(m3, 3, 3) };
struct Triple
{
int a, b, c;
Triple(int a, int b, int c)
: a(a), b(b), c(c)
{
}
};
int sum(const Triple& t)
{
return t.a + t.b + t.c;
}
void pythagoreanTriples(const Triple& root, int maxPerimeter, std::vector<Triple>& results)
{
// Recursively generates primitive triples using matrix multiplication.
if (sum(root) > maxPerimeter) {
return;
}
results.push_back(root);
double r[] = { root.a, root.b, root.c };
double c[3];
gsl_vector_view x = gsl_vector_view_array(r, 3);
gsl_vector_view y = gsl_vector_view_array(c, 3);
for (int i = 0; i < 3; ++i) {
gsl_blas_dgemv(CblasNoTrans, 1.0, &a[i].matrix, &x.vector, 0.0, &y.vector);
pythagoreanTriples(Triple(c[0], c[1], c[2]), maxPerimeter, results);
}
}
std::vector<Triple> pythagoreanTriples(int maxPerimeter)
{
std::vector<Triple> results;
pythagoreanTriples(Triple(3, 4, 5), maxPerimeter, results);
return results;
}
int rightestTrianglePerimeter(int maxPerimeter)
{
int result = -1;
std::vector<Triple> triples = pythagoreanTriples(maxPerimeter);
std::vector<int> vec(maxPerimeter / 2 + 1); // Only holds even values.
// Computes multiples of the sums of primitive triples.
for (int i = 0; i < triples.size(); ++i) {
int p = sum(triples[i]);
int m = p;
do {
++vec[m / 2];
m += p;
} while (m <= maxPerimeter);
}
int maxTriples = 0;
for (int i = 6; i < vec.size(); ++i) {
if (vec[i] > maxTriples) {
maxTriples = vec[i];
result = 2 * i;
}
}
return result;
}
int main()
{
std::cout << "Answer: " << rightestTrianglePerimeter(1000) << '\n';
return 0;
}