Note that we are only interested in integral pythagorean triplets, we are given the hypotenuse $c$, how can I efficiently find the other two sides of the right angled triangle. I need something better than the bruteforce approach of iterating over all lengths $a$ below $c$, and checking perfect square for $b = \sqrt{c^2-a^2}$.
For multiple solutions, I need one with the smallest $a$ possible.
- 1,329
-
Finding the other two sides with no other conditions except for the hypotenuse. – Juniven Acapulco May 11 '17 at 12:13
-
I think you want integer solutions - if so please edit the question to say so. – Ethan Bolker May 11 '17 at 12:15
-
Maybe this will help you. – Zubzub May 11 '17 at 12:16
-
1There are reasonable algorithms for finding all the ways to represent an integer as a sum of two squares. You can apply such an algorithm to the square of the given hypotenuse. See https://math.stackexchange.com/questions/366421/express-integer-as-sum-of-two-squares – Ethan Bolker May 11 '17 at 12:18
4 Answers
You cannot, because there exist infinitely many right angled triangles with the same hypotenuse length.
For example, if the length of the hypotenuse is $1$, then for every $x\in(0,1)$, $(x, \sqrt{1-x^2})$ are possible lengths of the other two sides.
- 126,227
- 6
- 135
- 211
-
I have just editted the question, in that case I need the answer with smallest possible value of $a$. – maverick May 11 '17 at 12:14
-
2@maverick There is no such thing as a smallest $a$, since you can always decrease it by half and still produce a right angled triangle. – 5xum May 11 '17 at 12:16
-
@maverick For example, if the hypotenuse is $c$, then for any $a, b$ such that $a$ and $b$ are side lengths, I can replace them with $\frac a2, \sqrt{c^2-b^2}$ and produce an "even smaller $a$". – 5xum May 11 '17 at 12:17
-
-
@5xum The OP asks about Pythagorean triples which, by definition, have integer sides and a right angle. I've shown him how to fine multiples, if they exist, and how to identify the smallest side before even seeing it. – poetasis Feb 20 '20 at 23:30
-
1@poetasis I answered the question before pythagorean triplets were mentioned in the question. The OP drastically changed the question by editing it after that... – 5xum Feb 21 '20 at 07:16
How about using the standard formula for generating Pythagorean triples? Solve $c = m^2 + n^2$ for $m$ and $n$. Then you have $a = m^2 - n^2$ and $b = 2mn$. (If $m$ and $n$ are co-prime and of opposite parity, the triple is primitive, otherwise not.) This requires less brute force than the approach you wanted to avoid, since $c < c^2$.
- 5,178
One could further sieve the search by casting $c=m^2+n^2$ over a modular base, for example base two as say $2z=4(x^2+2x+1)+4y^2$ which reduces to $z=2(x^2+y^2)+2(x+1)$. Clearly $z$ is also congruent to zero modulo two, so immediately here you get another four fold reduction in the bound to $z_1=x^2+y^2+x+1$. Since $c$ is known the more general sieve $az+r_z=(ax+r_x)^2+(ay+r_y)^2$ would allow brute force search reduction once you have solved $r_z\equiv r_x^2+r_y^2$. This may or may not be a good step depending on other factors like the totient of the base $a$. We could also look at $c+b=m^2+n^2+2mn=(m+n)^2$ which of course says that $b$ is $c$ less than a perfect square. So we would be checking perfect squares greater than $c$. There are many directions to go. I am not aware of a closed form solution to the number of pythagorean triples associated with a given hypotenuse however. I'm not sure there is one.
This is an easy problem. If $C\ne 4t+1, t\in\mathbb{N}$, there is no triple with that value.
Given Euclid's formula $\quad A=m^2-n^2,\quad B=2mn,\quad C=m^2+n^2$
$C= m^2+n^2\implies\space n=\sqrt{C-m^2}$ where $\biggl\lceil\sqrt{\frac{C}{2}}\space\space\biggr\rceil \le m\le\bigl\lfloor\sqrt{C}\bigr\rfloor$
Any $f(C,m)$ that yields an integer $n$ provides $f(m,n)$ for a Pythagorean triple.
For example, if $C=65,\biggl\lceil\sqrt{\frac{65}{2}}\space\space\biggr\rceil=6 \le m\le\bigl\lfloor\sqrt{65}\bigr\rfloor=8$ and we find
$$\sqrt{65-6^2}\notin \mathbb{N}\quad\qquad \sqrt{65-7^2}=4\qquad\qquad \sqrt{65-8^2}=1$$ Our two triples are: $$f(7,4)=(33,56,65)\quad and \quad f(8,1)=(63,16,65)$$
The one with the smallest possible $A$ will be the one with the smallest $m^2-n^2$.
- 6,795