Unique triples: Length of wire bent into triangle
이전 댓글 표시
I have attached a picture of the question for clarity.
My current code looks like this, I'm using a small range to test it out first. It's not working right though.
clear;
a=randi(10);
b=randi(10);
c=randi(10);
for L=1:20
if c = sqrt(a^2 + b^2);
L = a + b + c;
disp([a,b,c]);
else
disp([0,0,0]);
end;
end

댓글 수: 2
I like this statement at the end of the assigment "you may NOT use any of the built in Matlab functions for this question except the fprintf function.", noting that the question asks to "write a function".
I invite you to type
edit function
at the matlab prompt and look at the last line of the code that pops up.
채택된 답변
추가 답변 (1개)
Roger Stafford
2015년 4월 13일
편집: Roger Stafford
2015년 4월 13일
0 개 추천
The question is asking you to start with just a given integer, L, and find integers a, b, and c which are the lengths of a right triangle which sum to L. You need to write code that in some way searches through all possible combinations of integers a, b, and c that are valid lengths of such a triangle.
One way to do this would be with a couple of nested for-loops that go through all possible a and b integers less than L and check whether c = L-a-b satisfies the requirement for a right triangle.
Another way, which requires only one for-loop, would go through, say, all possible values of 'a' less than L/sqrt(2). Then from the two equations that must be satisfied, you can calculate uniquely what b and c must be, and you could then check whether they were positive integers or not.
If you are designing this code for the possibility of very large values of L, there would be a decided advantage in computing time in using the method with only one for-loop, even though it is more complicated.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!