How to check if 3 numbers are a Pythagorean triple?

1k Views Asked by At

How we could get three numbers without ordering and then check if they form a Pythagorean triple or not?

So, pythagorean(3, 4, 5) or pythagorean(5, 3, 4) will print/return true, while pythagorean(4, 3, 6) will print/return false.

2

There are 2 best solutions below

1
Alireza Asgarian On BEST ANSWER

You can use this algotrithm :

  #include<stdio.h>
  int main(){
long long int a, b, c ;
scanf("%llu %llu %llu", &a, &b, &c);

if (a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
{
    printf("YES");
}

else
    printf("NO");
   return 0;
   }
0
Mr_Doggus On

If you use the equation, a^2 + b^2 = c^2, c should be the largest number and the order of a and b should not matter. Just find the largest number, set that equal to c, and then set the other two values to a and b and check to see if the equality is true.