Finding pythagoras triples when only c is known
Show older comments
Finding a and b when c is known
Here's what I have
str='a=%4i b=%4i c=%4i';
for c=137
for a=1:200
for b=1:200
if c==sqrt(a^2+b^2)
disp(sprintf(str,a,b))
end
end
end
end
commandwindow
Accepted Answer
More Answers (3)
Rik
on 7 Apr 2019
You are quite close. The code below contains some optimization in terms of selection of values to check, and the check is modified to ignore float rounding errors.
clc
str='a=%4i b=%4i c=%4i\n';
for c=137
c2=c^2;
for a=1:(c-1)
a2=a^2;
for b=a:(c-1)
if abs(c2-(a2+b^2))<2*eps
fprintf(str,a,b,c)
end
end
end
end
5 Comments
dpb
on 7 Apr 2019
C is the squared value, Rik...don't want to square it.
John D'Errico
on 7 Apr 2019
Edited: John D'Errico
on 7 Apr 2019
DPB: NO. 137 is the square of NO integer. Rik is correct, though not as efficient as possible.
"If c = 137, how would I find the matching integers of 'a' and 'b' such that a^2 + b^2 = c^2?"
dpb
on 7 Apr 2019
My bad; misread indeed...if just so happens that 137 = 4^2 + 11^2 and I thunk that was the target value OP was looking for. But, I see that isn't what he actually wrote, indeed.
Mea culpa! :(
John D'Errico
on 7 Apr 2019
To me, it looked like one of those times whern I look in at 4:00 am, still a bit asleep. :)
dpb
on 7 Apr 2019
<vbg>
Somewhat more rudimentary would be
UP=fix(sqrt(Csq-1)); % can't be bigger ignoring degenerate case of (sqrt(Csq^2),0)
for i=1:UP
isq=i*i;
for j=1:UP
jsq=j*j;
if isq+jsq==Csq
disp([i j])
return
end
end
end
Since are dealing with integers don't need to worry about the floating point roundoff, at least as long as a double can still hold exact value for Csq (~15 decimal digits).
4 Comments
John D'Errico
on 7 Apr 2019
Edited: John D'Errico
on 7 Apr 2019
Nope. Here c was supplied, not c^2. Too early in the morning I think. ;-)
"If c = 137, how would I find the matching integers of 'a' and 'b' such that a^2 + b^2 = c^2?"
dpb
on 7 Apr 2019
Yeah, as noted above, that 137 is 4^2 + 11^2 made me misinterpret the input as being C^2 instead of C altho OP did write otherwise...thanks.
At least I did write Csq in the Answer...how one gets it is "exercise for the Student" :)
Rik
on 7 Apr 2019
Just out of curiosity: would you miss any values if you let the second loop start with i instead of 1?
(And to be honest I had already written the tolerance-safe version when I decided to cut the sqrt from the test. I anticipated sqrt might have introduced some float rounding errors.)
dpb
on 7 Apr 2019
No, it's exhaustive to make the second loop begin at the position of the first, yes. Do, of course, have to start at j==i, not i+1 as you note to get the double cases. But this will always find the two factors in the order of smaller, larger for unequal a,b (again as long as no precision overflow).
I forget just how yours was written now, first, Rik...seemed to me at the time that rounding wouldn't enter into the test because the only cases that would matter were perfect squares...so as long as didn't overflow the precision it would be exact and if it did, then the rounding test wouldn't help, anyway. But, I won't claim I was necessarily correct! :)
madhan ravi
on 7 Apr 2019
Edited: madhan ravi
on 7 Apr 2019
C2 = c^2;
[X,Y]=ndgrid(1:200);
XY=[X(:).^2,Y(:).^2];
XY(sum(XY,2)==C2,:) % first column is a and the second b which is squared, so if you apply sqrt() you get the values of a and b
Categories
Find more on Number Theory 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!