fork download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4.  
  5. void triples()
  6. {
  7. unsigned found = 0;
  8. unsigned tests = 0;
  9. for (unsigned a = 3; a < 99; ++a)
  10. {
  11. // only interested in a hypotenuse <= 100
  12. const unsigned b_limit = std::sqrt(100*100 - (a*a));
  13.  
  14. for (unsigned b = a + 1; b <= b_limit; b += 1)
  15. {
  16. ++tests;
  17. double c = std::sqrt(a*a + b*b);
  18.  
  19. if (c == int(c))
  20. {
  21. ++found;
  22. std::cout << a << '\t' << b << '\t' << c << '\n';
  23. }
  24. }
  25. }
  26. std::cout << "Triples found: " << found << '\n';
  27. std::cout << "Possibilities tested: " << tests << '\n';
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33. triples();
  34. }
  35.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
3	4	5
5	12	13
6	8	10
7	24	25
8	15	17
9	12	15
9	40	41
10	24	26
11	60	61
12	16	20
12	35	37
13	84	85
14	48	50
15	20	25
15	36	39
16	30	34
16	63	65
18	24	30
18	80	82
20	21	29
20	48	52
21	28	35
21	72	75
24	32	40
24	45	51
24	70	74
25	60	65
27	36	45
28	45	53
28	96	100
30	40	50
30	72	78
32	60	68
33	44	55
33	56	65
35	84	91
36	48	60
36	77	85
39	52	65
39	80	89
40	42	58
40	75	85
42	56	70
45	60	75
48	55	73
48	64	80
51	68	85
54	72	90
57	76	95
60	63	87
60	80	100
65	72	97
Triples found: 52
Possibilities tested: 3647