Project Euler task 39

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 < iostream >
#include < math.h >

using namespace std;

int piSqr[500];


int main() {
double fSqrt22 = sqrt((double) 2) / ((double) 2);

for (int i = 0; i < 500; i++)
piSqr[i] = i * i;

int iMaxVariants = 0;
int iPMax = 0;
for (int p = 3; p <= 1000; p++) {
int iVariants = 0;
for (int c = 1; c < p / 2; c++)
for (int a = 1; a < (int) (fSqrt22 * (double) c); a++) {
int b = p - a - c;

if (piSqr[a] + piSqr[b] == piSqr[c])
iVariants++;
}

if (iVariants > iMaxVariants) {
iMaxVariants = iVariants;
iPMax = p;
}
}

cout << "p: " << iPMax << endl;

return 0;
}

Comments

Popular Posts