-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler009.cpp
More file actions
67 lines (48 loc) · 1.25 KB
/
Copy pathprojectEuler009.cpp
File metadata and controls
67 lines (48 loc) · 1.25 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
/* Problem 9
Special Pythagorean Triplet
A Pythagorean triplet is a set of three natural numbers a < b < c for which
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
*/
#include <iostream>
using namespace std;
void main () {
float abcSum = 1000;
float cMin = abcSum / 3;
bool foundTriple = false;
int c_initial = floor (cMin) + 1;
int a_initial = c_initial - 2;
int b_initial = c_initial - 1;
// Make sure initial a + b + c = abcSum
if (c_initial + a_initial + b_initial != abcSum) {
c_initial++;
}
int c = c_initial;
int a = a_initial;
int b = b_initial;
int bCounter = 0;
while (!foundTriple && bCounter < floor (float(a_initial) / 2)) {
c = c_initial + bCounter;
a = a_initial - (bCounter * 2);
b = b_initial + bCounter;
while (a > 0) {
int a2 = a * a;
int b2 = b * b;
int c2 = c * c;
if (a2 + b2 == c2) {
foundTriple = true;
break;
}
a--;
c++;
}
bCounter++;
}
cout << "found? " << foundTriple << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << "The product abc = " << a * b * c << endl;
}