Checking for Pythagoric Triplets

조회 수: 11 (최근 30일)
Alex
Alex 2015년 1월 14일
댓글: John D'Errico 2015년 1월 14일
I need to make a function that checks if an input vector of 3 numbers is a pythagoric triple. now the condition that stops me is that the function can only check the numbers once, i.e i cant make it check a^2+b^2=c^2 and then check for a^2+c^2=b^2 (input numbers can be random). and then i need to find all triples between two random numbers. Pretty much lost here so any help would be awesome.
edit: i figured out how to check for the numbers , but still having trouble finding all triples between lets say 2 and 20 or 5 and 40...

답변 (3개)

John D'Errico
John D'Errico 2015년 1월 14일
I'm confused. If the numbers can be in any order, then just sort them first and then test! I'm pretty sure that if this expression
a^2 + b^2 = c^2
holds true, then c must be the largest of the three numbers, at least if we are dealing with positive real numbers. Maybe the new math has changed that. I don't know.
As for finding all triples between two numbers, you need to more completely define what you mean by that. Anyway, a set of numbers, once provided to you, are not random. They may be arbitrary, but that is very different from random.
  댓글 수: 3
John D'Errico
John D'Errico 2015년 1월 14일
편집: John D'Errico 2015년 1월 14일
Sigh. I know what a pair of numbers is.
That does not answer my question. What does it mean for a triplet to lie between two numbers? You need to define the problem accurately enough that I can answer! Do you require that each of the values a,b,c all lie between a given upper and lower bound? Or only c perhaps? Or something else that I have not guessed? The fact is, if I try to guess, I will guess wrongly. This is a given, an absolute.
If I had to guess, I might decide that you need to find all triples {a,b,c} such that
L <= a,b,c <= U
and where
a^2 + b^2 = c^2
If that is so, then you might start with Euclid's formula. Find it here , or look here .
John D'Errico
John D'Errico 2015년 1월 14일
You still have not answered my question. Does a pythagorean triple in that interval mean what I conjectured I does?

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2015년 1월 14일
편집: Sean de Wolski 2015년 1월 14일
This should work:
isPythTrip = @(x)any(any(bsxfun(@eq,(bsxfun(@(x,y)hypot(x,y),x.',x)),circshift(x,[0 1]))))
isPythTrip([3 4 5])
ans =
1
isPythTrip([3 5 4])
ans =
1
isPythTrip([3 5 6])
ans =
0
Of course, there are prettier and safer ways...
  댓글 수: 1
Alex
Alex 2015년 1월 14일
figured that part already by sorting the vector, the question is how i find tirples between two given numbers as in 2 and 20

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2015년 1월 14일
Since there are infinitely many real-valued "Pythagoric" triplets between two given limits, 'k' and 'n', I will guess that you actually mean that their values are to be restricted to integer values. To do that, it is preferable to first assume that the three values are in ascending order: a <= b <= c. All other triplets can be obtained from permutations of these. The following code will obtain all such ascending triplets with values between k and n in the rows of A:
A = [];
for a = k:n
for b = a:n
t = (b:min(n,a+b)).';
A = [A;[repmat([a,b],length(t),1),t]];
end
end
[Note: I didn't manage to find a simple formula for the number of such triplets in terms of k and n to allow for an exact initial allocation. To speed up the above code you could allocate some sufficiently large N-by-3 size for A, keep a count of the number of entries made by cumulating the length(t) values, and then delete the unused part of A.]

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by