Unique triples: Length of wire bent into triangle
Show older comments
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 Comments
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.
Accepted Answer
More Answers (1)
Roger Stafford
on 13 Apr 2015
Edited: Roger Stafford
on 13 Apr 2015
0 votes
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.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!