Special Pythagorean triplet | Project Euler | Problem #9
URL to the problem page: https://projecteuler.net/problem=9
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.
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.
#include <iostream>
using namespace std;
long long power(long long a, long long b) {
long long result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
}
int main()
{
long long i, j, k, exit = 0, exit2 = 0;
for (i = 0; i < 1000; i++) {
for (j = (i + 1); j < 1000; j++) {
for (k = (j + 1); k < 1000; k++) {
if ((power(i, 2) + power(j, 2)) == power(k, 2) && (i + j + k) == 1000) {
cout << "Product a.b.c (where a, b and c is a Pythagorean triplet for which a + b + c = 1000) is = " << i * j * k << endl;
exit++;
break;
}
}
if (exit != 0) {
exit2++;
break;
}
}
if (exit2 != 0) {
break;
}
}
return 0;
Comments
Post a Comment