尤拉計劃問題九matlab實現
阿新 • • 發佈:2018-11-10
Problem 9 : Special Pythagorean triplet(特殊的畢達哥拉斯三重奏)
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
For example,
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
思路 :
這題的話思路很簡單,用for迴圈去實現,定義三個迴圈變數a,b,c,若滿足以下條件:
就把滿足條件的三個數相乘,乘積賦值給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