Pythagorean Triples with Loops
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2020-3-18
What have you done so far? What specific problems are you having with your code?
回答(1 个)
James Tursa
2020-3-18
编辑:James Tursa
2020-3-18
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 个评论
Eric
2024-2-21
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
2024-2-21
Use them as the loop variables as James has done above and update the output variable accordingly.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assembly 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!