Find pairs of side integers for a given hypothenuse number so it is Pythagorean Triple

305 Views Asked by At

I am trying to find all pairs of side integers (a, b) for a given hypothenuse number n so that (a, b, n) is a Pythagorean triple, i.e.,$ a^2 + b^2 = n^2$

The approach i am using is

  1. Sorting the array in ascending order
  2. Finding the square of each element in array

for(j->0 to (a.length-1)) for (i->j+1 to (a.length-1)) search the a[j]+a[i] ahead in the array from i+1 to the end of array if found get the triplet according to sqrt(a[j]),sqrt(a[i]) & sqrt(a[j]+a[o]) end end

Is this approach correct?

1

There are 1 best solutions below

0
On BEST ANSWER

First of all let us keep the traditional $a\lt b\lt c$ as three sides of right angle triangle and $(a+b)\gt c$ (c ,being the hypothenuse)

ie., (a+b)=(c+d)

$ a = k(m^2 -n^2)$

b = k(2mn)

$ c = k(m^2 +n^2)$

$ d = k[{2n(m-n)}^2]$ for $m\gt n$ and interestingly

the sum of squares of two consecutive natural numbers generate the primes of hypothenuse numbers and hence say m=n+1 then ' c ' will become

$ *0 c = k[ 2(n^2) + 2n + 1 ]$

$ * (c-1)/2 = k{n(n+1)}$

With given 'c' now you can find out a & b easily.