Advertisement
makispaiktis

Project Euler 9 - Pythagorean Triplet

Apr 28th, 2020 (edited)
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /*
  2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
  3. a^2 + b^2 = c^2
  4. For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
  5. There exists exactly one Pythagorean triplet for which a + b + c = 1000.
  6. Find the product a*b*c.
  7. */
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #define LIMIT 1000
  13.  
  14. using namespace std;
  15.  
  16. // Auxiliary Functions
  17. bool isPythagorianTriplete(int a, int b, int c){
  18.     if(c == sqrt(a*a + b*b)){
  19.         return true;
  20.     }
  21.     return false;
  22. }
  23.  
  24. int main()
  25. {
  26.     int a, b, c;
  27.     for(int i=0; i<LIMIT; i++){
  28.         for(int j=0; j<LIMIT; j++){
  29.             for(int k=0; k<LIMIT; k++){
  30.                 if(isPythagorianTriplete(i, j, k) == true && i < j && j < k && i+j+k == 1000){
  31.                     a = i;
  32.                     b = j;
  33.                     c = k;
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     cout << "These numbers are: " << a << ", " << b << " and " << c << ", so " << a << " * " << b << " * " << c << " = " << a * b * c << endl;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement