/
009.cpp
60 lines (53 loc) · 1.1 KB
/
009.cpp
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
/*
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>
#include <cmath>
int compute(int a, int b, int c)
{
if( a + b + c == 1000 )
{
return a*b*c;
}
return 0;
}
bool checkPitagorean(int a, int b, int c)
{
if(a < b)
{
if(b < c)
{
if( pow(a,2) + pow(b,2) == pow(c,2) )
{
return true;
}
}
}
return false;
}
int main()
{
for(int a = 0; a < 1000; ++a)
{
for(int b = 0; b < 1000; ++b)
{
for(int c = 0; c < 1000; ++c)
{
if( checkPitagorean(a, b, c) )
{
int temp = compute(a, b, c);
if(temp != 0)
{
std::cout << temp << "\n";
return 0;
}
}
}
}
}
return 1;
}