问题:
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
答案:
31875000
Matlab:%c=1000-a-b
%a^2 + b^2 = (1000-a-b)^2
%b=(1000^2/2-1000*a)/(1000-a) > a
%a can be defined a< 1000-1000/2^.5 = 292
a=0; b=0 c=0;
f=0; %get out from loops
for a=1:292
for b=a+1:floor((1000-a)/2)
if a^2+b^2==(1000-a-b)^2
f=1;
c=1000-a-b;
break;
end
end
if f==1
break;
end
end
a
b
c
sum=a*b*c
%The answer is 3187500
阅读(446) | 评论(0) | 转发(0) |