Checking for Pythagoric Triplets
8 次查看(过去 30 天)
显示 更早的评论
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...
0 个评论
回答(3 个)
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
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
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
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...
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.]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!