fork(2) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5. struct triple_t
  6. {
  7. unsigned x;
  8. unsigned y;
  9. unsigned z;
  10. };
  11.  
  12. std::ostream & operator<<(std::ostream & out, triple_t const & triple)
  13. {
  14. return out << triple.x << ", " << triple.y << ", " << triple.z;
  15. }
  16.  
  17. class triple_iterator_t : public std::iterator<std::forward_iterator_tag, triple_t>
  18. {
  19. private:
  20. triple_t triple_;
  21.  
  22. static triple_t next(unsigned x, unsigned y, unsigned z)
  23. {
  24. while (true)
  25. {
  26. while (true)
  27. {
  28. while (true)
  29. {
  30. if (y == z) break;
  31. ++y;
  32. if (x * x + y * y == z * z) return {x, y, z};
  33. }
  34. if (x == z) break;
  35. ++x;
  36. y = x;
  37. }
  38. ++z;
  39. x = 1;
  40. }
  41. }
  42.  
  43. public:
  44. triple_iterator_t() : triple_{1, 1, 1}
  45. {
  46. ++(*this);
  47. }
  48.  
  49. triple_t operator *()
  50. {
  51. return triple_;
  52. }
  53.  
  54. triple_iterator_t & operator ++()
  55. {
  56. triple_ = next(triple_.x, triple_.y, triple_.z);
  57. return *this;
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. copy_n(triple_iterator_t(), 10, std::ostream_iterator<triple_t>(std::cout, "\n"));
  64. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
3, 4, 5
6, 8, 10
5, 12, 13
9, 12, 15
8, 15, 17
12, 16, 20
7, 24, 25
15, 20, 25
10, 24, 26
20, 21, 29