fork 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_{3, 4, 5};
  21.  
  22. static triple_t next(unsigned x, unsigned y, unsigned z)
  23. {
  24. while (true)
  25. {
  26. if (++y == z)
  27. {
  28. if (++x == y)
  29. {
  30. ++z;
  31. x = 1;
  32. }
  33. y = x;
  34. }
  35.  
  36. if (x * x + y * y == z * z) return {x, y, z};
  37. }
  38. }
  39.  
  40. public:
  41. triple_t operator *()
  42. {
  43. return triple_;
  44. }
  45.  
  46. triple_iterator_t & operator ++()
  47. {
  48. triple_ = next(triple_.x, triple_.y, triple_.z);
  49. return *this;
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. copy_n(triple_iterator_t(), 10, std::ostream_iterator<triple_t>(std::cout, "\n"));
  56. }
Success #stdin #stdout 0s 3296KB
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