Problem 9
Statement
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.
Solution
The code, in C.
#include <stdio.h>
int pythTrip(void)
{
int a,b,c,k,m,n;
for(n=1;n<100;n++){
for(m=n;m<100;m++){
for(k=1;k<100;k++){
a = k*2*m*n;
b = k*(m*m-n*n);
c = k*(m*m+n*n);
if(a+b+c==1000)
return a*b*c;
}
}
}
}
int main(void)
{
printf("%d\n",pythTrip());
return 0;
}
Answer
31875000
SPEAK / ADD YOUR COMMENT
Comments are moderated.
Posts