Pythagorean Triples with Loops

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

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.

3 Comments

Thank You! I will continue to work on it and see where I get.
I'm a little late but how exactly would I save the values of a, b, and c in a matrix and then create new values of a, b, and c to check for a scaled duplicate. I have in my code SideLenghtMat = [a b c]; but after another triangle is found and the values of a b and c are different how would I check the old values of a b and c. I guess my question is: how do I save each unique value of a b and c without them being erased from the system every time the loop happens to check for a triple?
Use them as the loop variables as James has done above and update the output variable accordingly.

Sign in to comment.

Categories

Asked:

on 18 Mar 2020

Commented:

on 21 Feb 2024

Community Treasure Hunt

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

Start Hunting!