欧拉计划问题九matlab实现

Problem 9 : Special Pythagorean triplet(特殊的毕达哥拉斯三重奏)

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

                                              \large a^2 + b^2 = c^2

For example,            \large 3^2 + 4^2 = 9 +16 = 25 = 5^2

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

思路 :

这题的话思路很简单,用for回圈去实现,定义三个循环变量a,b,c,若满足以下条件:

  • \large a^2 + b^2 = c^2
  • \large a + b + c =1000

就把满足条件的三个数相乘,乘积赋值给S,最后S就是输出结果。

代码 :

tic
for a = 1:1000
    for b = 1:1000
        for c = 1:1000
            if a^(2) + b^(2) == c^(2) && a + b + c == 1000
                s = a*b*c;
                %%disp(a),disp(b),disp(c);
                disp(s)
            end
        end
    end
end
toc

结果 :31875000

希望大家多多交流!

猜你喜欢

转载自blog.csdn.net/qq_38910271/article/details/82993917