MATLAB Answers

Pythagorean Triples with Loops

5 views (last 30 days)
Adam Olsen
Adam Olsen on 18 Mar 2020
Edited: James Tursa on 18 Mar 2020
I need help figuring out how to write this. I have a general idea of what to write, but I am stuck with what the general form of the script should be. Also, how to eliminate the duplicates. Any help would be appreciated. Thank You!

  1 Comment

James Tursa
James Tursa on 18 Mar 2020
What have you done so far? What specific problems are you having with your code?

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 18 Mar 2020
Edited: James Tursa on 18 Mar 2020
E.g., an outline
% (1) insert code here to ask for the largest value n
for a=1:n
for b=a:n
for c=b:n
% (2) insert code here to check for a^2 + b^2 = c^2
% (3) if it works, save the a, b, c values in a matrix (e.g., one of the rows)
end
end
end
% (4) write code here to eliminate scaled duplicates
Write (1) first to make sure you can input a positive integer.
Write the logic for (2) next. When you get a set that works, just print it to the screen for now.
When you have (2) working, write (3) to store the results in a matrix (e.g., one set per row).
When you get (3) working, write code for (4) to eliminate scaled duplicates. Or, you could move (4) right next to (3) and check for scaled duplicates as you go and not even include them in the matrix.

  1 Comment

Adam Olsen
Adam Olsen on 18 Mar 2020
Thank You! I will continue to work on it and see where I get.

Sign in to comment.