I have a mistake in loops

3 ビュー (過去 30 日間)
sznailc
sznailc 2021 年 11 月 5 日
編集済み: Stephen23 2021 年 11 月 5 日
Hello, I have somewhere a mistake that if N = 1000 the output misses some lines (don't know which), so I suppose there's a mistake somewhere in for loops. It works correctly with N = 50, 100, 500.
function [R, I] = problem2B(N)
I = 0;
for a = 3:N
for b = a:N
for c = b:N
if (a^2 + b^2) == c^2 && a+b+c < N
R(I+1, :) = [a+b+c, a, b, c];
I = I + 1;
elseif N < 12
R = zeros(4, 1);
end
end
end
end
if N <= 50
R = reshape(nonzeros(R), I, 4);
else
R = sortrows(R, 1);
end
end
  3 件のコメント
sznailc
sznailc 2021 年 11 月 5 日
編集済み: sznailc 2021 年 11 月 5 日
I have a test file that says, that not all possible pythagorean triples are displayed with N = 1000. Therefore I think there has to be a mistake with indexes that it skips some possible triples.
Stephen23
Stephen23 2021 年 11 月 5 日
編集済み: Stephen23 2021 年 11 月 5 日
Note that if you change to
R = zeros(0,4);
then you can get rid of NONZEROS and RESHAPE.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2021 年 11 月 5 日
Does your problem ask you to find all Pythagorean triples whose elements sum to less than 1000 or all whose elements sum to less than or equal to 1000?
  1 件のコメント
sznailc
sznailc 2021 年 11 月 5 日
wow, thank you, edited to a+b+c <= N. I couldn't tell what triple is missing and didn't know that it might be 1000

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by