Advertisement
makispaiktis

Euler Brick Problem

Apr 17th, 2020 (edited)
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #define LIMIT 500
  7.  
  8. using namespace std;
  9.  
  10. // Auxiliary Functions
  11. double hypot(int a, int b){
  12.     return sqrt(a*a + b*b);
  13. }
  14.  
  15. bool isInteger(double d){
  16.     if(d == int(d)){
  17.         return true;
  18.     }
  19.     return false;
  20. }
  21.  
  22. // MAIN FUNCTION
  23. int main()
  24. {
  25.     cout << "LIMIT = " << LIMIT << ", for a, b and c" << endl << endl;
  26.     cout << "Time for 250^3 executions is about 2.1 seconds." << endl;
  27.     cout << "Time for 500^3 executions is about 16.8 seconds." << endl;
  28.     cout << "Time for 750^3 executions is about 52 seconds." << endl;
  29.     cout << "Time for 1000^3 executions is about 125 seconds." << endl << endl;
  30.     vector <int> triades = vector <int> ();
  31.     int counter = 0;
  32.     for(int a=1; a<LIMIT; a++){
  33.         for(int b=1; b<LIMIT; b++){
  34.             for(int c=1; c<LIMIT; c++){
  35.                 double hypot1 = hypot(a, b);
  36.                 double hypot2 = hypot(a, c);
  37.                 double hypot3 = hypot(b, c);
  38.                 if(isInteger(hypot1) == true && isInteger(hypot2) == true && isInteger(hypot3) == true && (hypot1 < hypot2) && (hypot2 < hypot3)){
  39.                     counter++;
  40.                     triades.push_back(a);
  41.                     triades.push_back(b);
  42.                     triades.push_back(c);
  43.                     cout << "**** Solution " << counter << " ****" << endl;
  44.                     cout << "(" << a << ", " << b << ", " << c << "), because: " << endl;
  45.                     cout << "x = sqrt(" << a << "^2 + " << b << "^2 = " << hypot1 << endl;
  46.                     cout << "y = sqrt(" << a << "^2 + " << c << "^2 = " << hypot2 << endl;
  47.                     cout << "z = sqrt(" << b << "^2 + " << c << "^2 = " << hypot3 << endl << endl;
  48.                 }
  49.             }   // END OF FOR-LOOP-3
  50.         }   // END OF FOR-LOOP-2
  51.     }   // END OF FOR-LOOP-1
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement