Project Euler - problem 009

August 1st, 2007 by Daniel

A Pythagorean triplet is a set of three natural numbers, a<b<c, for which,
a² + b² = c²

For example, 3² + 4² = 9 + 16 = 25 = 5².

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

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
 
 
#include &lt;iostream&gt;
using namespace std;
 
#define limit 1000
 
//declare methods
bool isPythagoreanTriplet(int a, int b, int c);
 
 
int main ()
{
int res(1), a(0), b(0), c(0);
 
while(!(a + b + c == limit &amp;&amp; isPythagoreanTriplet(a,b,c)) &amp;&amp; a &lt; limit){
a++;
b= 0;
while(!(a + b + c == limit &amp;&amp; isPythagoreanTriplet(a,b,c)) &amp;&amp; b &lt; limit){
b++;
c= 0;
while(!(a + b + c == limit &amp;&amp; isPythagoreanTriplet(a,b,c) ) &amp;&amp; c &lt; limit){
c++;
}
}
}
res = a*b*c;
 
cout &lt;&lt; "resultatet er ";
cout &lt;&lt; res &lt;&lt; "\n";
 
return 0;
}
 
 
bool isPythagoreanTriplet(int a, int b, int c){
return (a*a + b*b == c*c);
}

0 Responses to “Project Euler - problem 009”

  1. No Comments

Leave a Response