Pythagorean Triples with Loops

4 visualizaciones (últimos 30 días)
Adam Olsen
Adam Olsen el 18 de Mzo. de 2020
Comentada: Dyuman Joshi el 21 de Feb. de 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 comentario
James Tursa
James Tursa el 18 de Mzo. de 2020
What have you done so far? What specific problems are you having with your code?

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 18 de Mzo. de 2020
Editada: James Tursa el 18 de Mzo. de 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 comentarios
Eric
Eric el 21 de Feb. de 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 el 21 de Feb. de 2024
Use them as the loop variables as James has done above and update the output variable accordingly.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by