1234567891011121314151617181920212223242526272829303132333435363738394041 |
- /* Given three numbers, x, y and z,
- * determine whether they are the edges of a right angled triangle.
- * Notice the largest side of the triangle might not be the last one
- * passed to the function. All numbers will be integers (whole numbers).
- */
- // two soltions, one nice solution...One longer solution that led to the
- // nice solution.
- bool rightTriangle(int x, int y, int z) {
- std::vector<int> valid = {x,y,z};
- for(int i=0;i<valid.size();i++){
- if(valid[i]<=0){
- return false;
- }
- valid[i]*=valid[i];
- }
- std::sort(valid.begin(),valid.end());
- return valid[2] == valid[0]+valid[1];
-
- /* long int largest = z;
- long int sideOne = x;
- long int sideTwo = y;
- // if any x,y,z is 0: return false. if any x,y,z is negative: return false.
- if((x<=0) || (y<=0) || (z<=0)){
- return false;
- }
- // find largest size
- if (x>y){
- if(x>z){
- sideOne = y;
- sideTwo = z;
- largest = x;
- }
- }
- else
- if(y>z){
- sideTwo = z;
- largest = y;
- }
- return (largest*largest) == ((sideOne*sideOne) + (sideTwo*sideTwo));
- */
- }
|