fork download
  1. #include <iostream>
  2. #include <cmath>
  3. int main()
  4. {
  5. int a , b , c;
  6. //a < b < c
  7. //a^2 + b^2 = c^2
  8. //n = 60 so sqrt is about 7
  9. //const int sq_rt = 7;
  10. const int n = 1000;
  11. const int sq_rt = sqrt( n );
  12. for( c = sq_rt; c > 4; --c ) //worst case c == 5 when b == 4 and a == 3
  13. {
  14. for( b = c - 1; b > 3; --b ) //worst case b == 4 when a == 3
  15. {
  16. for( a = b - 1; a > 2; --a ) //worst case a == 3
  17. {
  18. if( a * a + b * b == c * c )
  19. {
  20. std::cout << "Triplet: a = " << a << " b = " << b << " c = " << c << std::endl;
  21. std::cout << "Triplet value = " << c * c << std::endl;
  22. }
  23. }
  24. }
  25. }
  26. return 0;
  27. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Triplet: a = 18 b = 24 c = 30
Triplet value = 900
Triplet: a = 20 b = 21 c = 29
Triplet value = 841
Triplet: a = 10 b = 24 c = 26
Triplet value = 676
Triplet: a = 7 b = 24 c = 25
Triplet value = 625
Triplet: a = 15 b = 20 c = 25
Triplet value = 625
Triplet: a = 12 b = 16 c = 20
Triplet value = 400
Triplet: a = 8 b = 15 c = 17
Triplet value = 289
Triplet: a = 9 b = 12 c = 15
Triplet value = 225
Triplet: a = 5 b = 12 c = 13
Triplet value = 169
Triplet: a = 6 b = 8 c = 10
Triplet value = 100
Triplet: a = 3 b = 4 c = 5
Triplet value = 25