Pythagorean Triples with Loops

5 Ansichten (letzte 30 Tage)
Adam Olsen
Adam Olsen am 18 Mär. 2020
Kommentiert: Dyuman Joshi am 21 Feb. 2024
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 Kommentar
James Tursa
James Tursa am 18 Mär. 2020
What have you done so far? What specific problems are you having with your code?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

James Tursa
James Tursa am 18 Mär. 2020
Bearbeitet: James Tursa am 18 Mär. 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 Kommentare
Eric
Eric am 21 Feb. 2024
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?
Dyuman Joshi
Dyuman Joshi am 21 Feb. 2024
Use them as the loop variables as James has done above and update the output variable accordingly.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by