-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler039.cpp
More file actions
43 lines (32 loc) · 900 Bytes
/
Copy pathprojectEuler039.cpp
File metadata and controls
43 lines (32 loc) · 900 Bytes
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
/* Problem 39
Integer Right Triangles
If p is the perimeter of a right angle triangle with integral length sides,
{a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximised?
*/
#include <cmath>
#include <iostream>
using namespace std;
int main () {
int maxNumSolutions = 0;
int maxP = 0;
for (int p = 2; p <= 1000; p++) {
int numSolutions = 0;
for (int i = 1; i < p; i++) {
for (int j = 1; j < p; j++) {
double k = sqrt (pow (double (i), 2) + pow (double (j), 2));
if (floor (k) == k) {
if (i + j + k == p) {
numSolutions++;
}
}
}
}
if (numSolutions > maxNumSolutions) {
maxP = p;
maxNumSolutions = numSolutions;
}
}
cout << maxP << " had " << maxNumSolutions << " solutions." << endl;
}