code.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* Given three numbers, x, y and z,
  2. * determine whether they are the edges of a right angled triangle.
  3. * Notice the largest side of the triangle might not be the last one
  4. * passed to the function. All numbers will be integers (whole numbers).
  5. */
  6. // two soltions, one nice solution...One longer solution that led to the
  7. // nice solution.
  8. bool rightTriangle(int x, int y, int z) {
  9. std::vector<int> valid = {x,y,z};
  10. for(int i=0;i<valid.size();i++){
  11. if(valid[i]<=0){
  12. return false;
  13. }
  14. valid[i]*=valid[i];
  15. }
  16. std::sort(valid.begin(),valid.end());
  17. return valid[2] == valid[0]+valid[1];
  18. /* long int largest = z;
  19. long int sideOne = x;
  20. long int sideTwo = y;
  21. // if any x,y,z is 0: return false. if any x,y,z is negative: return false.
  22. if((x<=0) || (y<=0) || (z<=0)){
  23. return false;
  24. }
  25. // find largest size
  26. if (x>y){
  27. if(x>z){
  28. sideOne = y;
  29. sideTwo = z;
  30. largest = x;
  31. }
  32. }
  33. else
  34. if(y>z){
  35. sideTwo = z;
  36. largest = y;
  37. }
  38. return (largest*largest) == ((sideOne*sideOne) + (sideTwo*sideTwo));
  39. */
  40. }