11// Range v3 library
22//
3- // Copyright Eric Niebler 2013-2014
3+ // Copyright Eric Niebler 2013-present
44//
55// Use, modification and distribution is subject to the
66// Boost Software License, Version 1.0. (See accompanying
1616
1717using namespace ranges ;
1818
19- int main ()
19+ int
20+ main ()
2021{
21- // Define an infinite range containing all the Pythagorean triples:
22- auto triples =
23- view::for_each (view::ints (1 ), [](int z)
24- {
25- return view::for_each (view::ints (1 , z + 1 ), [=](int x)
26- {
27- return view::for_each (view::ints (x, z + 1 ), [=](int y)
28- {
29- return yield_if (x*x + y * y == z * z, std::make_tuple (x, y, z));
30- });
22+ // Define an infinite range containing all the Pythagorean triples:
23+ auto triples = views::for_each (views::iota (1 ), [](int z) {
24+ return views::for_each (views::iota (1 , z + 1 ), [=](int x) {
25+ return views::for_each (views::iota (x, z + 1 ), [=](int y) {
26+ return yield_if (x * x + y * y == z * z,
27+ std::make_tuple (x, y, z));
28+ });
29+ });
3130 });
32- });
3331
34- // // This alternate syntax also works:
35- // auto triples = ints (1) >>= [] (int z) { return
36- // ints (1, z+1) >>= [=](int x) { return
37- // ints (x, z+1) >>= [=](int y) { return
38- // yield_if(x*x + y*y == z*z,
39- // std::make_tuple(x, y, z)); };}; };
32+ // // This alternate syntax also works:
33+ // auto triples = iota (1) >>= [] (int z) { return
34+ // iota (1, z+1) >>= [=](int x) { return
35+ // iota (x, z+1) >>= [=](int y) { return
36+ // yield_if(x*x + y*y == z*z,
37+ // std::make_tuple(x, y, z)); };}; };
4038
41- // Display the first 100 triples
42- RANGES_FOR (auto triple, triples | view::take (100 ))
43- {
44- std::cout << ' ('
45- << std::get<0 >(triple) << ' ,'
46- << std::get<1 >(triple) << ' ,'
47- << std::get<2 >(triple) << ' )' << ' \n ' ;
48- }
39+ // Display the first 100 triples
40+ RANGES_FOR (auto triple, triples | views::take (100 ))
41+ {
42+ std::cout << ' (' << std::get<0 >(triple) << ' ,' << std::get<1 >(triple)
43+ << ' ,' << std::get<2 >(triple) << ' )' << ' \n ' ;
44+ }
4945}
5046
5147// //////////////////////////////////////////////////////////////////////////////////////////////////
@@ -55,75 +51,74 @@ int main()
5551class timer
5652{
5753private:
58- std::chrono::high_resolution_clock::time_point start_;
54+ std::chrono::high_resolution_clock::time_point start_;
55+
5956public:
60- timer ()
61- {
62- reset ();
63- }
64- void reset ()
65- {
66- start_ = std::chrono::high_resolution_clock::now ();
67- }
68- std::chrono::milliseconds elapsed () const
69- {
70- return std::chrono::duration_cast<std::chrono::milliseconds>(
71- std::chrono::high_resolution_clock::now () - start_);
72- }
73- friend std::ostream &operator <<(std::ostream &sout, timer const &t)
74- {
75- return sout << t.elapsed ().count () << " ms" ;
76- }
57+ timer ()
58+ {
59+ reset ();
60+ }
61+ void reset ()
62+ {
63+ start_ = std::chrono::high_resolution_clock::now ();
64+ }
65+ std::chrono::milliseconds elapsed () const
66+ {
67+ return std::chrono::duration_cast<std::chrono::milliseconds>(
68+ std::chrono::high_resolution_clock::now () - start_);
69+ }
70+ friend std::ostream &operator <<(std::ostream &sout, timer const &t)
71+ {
72+ return sout << t.elapsed ().count () << " ms" ;
73+ }
7774};
7875
79- void benchmark ()
76+ void
77+ benchmark ()
8078{
81- // Define an infinite range containing all the Pythagorean triples:
82- auto triples =
83- view::for_each (view::ints (1 ), [](int z)
84- {
85- return view::for_each (view::ints (1 , z + 1 ), [=](int x)
86- {
87- return view::for_each (view::ints (x, z + 1 ), [=](int y)
88- {
89- return yield_if (x*x + y * y == z * z, std::make_tuple (x, y, z));
90- });
79+ // Define an infinite range containing all the Pythagorean triples:
80+ auto triples = views::for_each (views::iota (1 ), [](int z) {
81+ return views::for_each (views::iota (1 , z + 1 ), [=](int x) {
82+ return views::for_each (views::iota (x, z + 1 ), [=](int y) {
83+ return yield_if (x * x + y * y == z * z,
84+ std::make_tuple (x, y, z));
85+ });
86+ });
9187 });
92- });
9388
94- static constexpr int max_triples = 3000 ;
89+ static constexpr int max_triples = 3000 ;
9590
96- timer t;
97- int result = 0 ;
98- RANGES_FOR (auto triple, triples | view ::take (max_triples))
99- {
100- int i, j, k;
101- std::tie (i, j, k) = triple;
102- result += (i + j + k);
103- }
104- std::cout << t << ' \n ' ;
105- std::cout << result << ' \n ' ;
91+ timer t;
92+ int result = 0 ;
93+ RANGES_FOR (auto triple, triples | views ::take (max_triples))
94+ {
95+ int i, j, k;
96+ std::tie (i, j, k) = triple;
97+ result += (i + j + k);
98+ }
99+ std::cout << t << ' \n ' ;
100+ std::cout << result << ' \n ' ;
106101
107- result = 0 ;
108- int found = 0 ;
109- t.reset ();
110- for (int z = 1 ;; ++z)
111- {
112- for (int x = 1 ; x <= z; ++x)
102+ result = 0 ;
103+ int found = 0 ;
104+ t.reset ();
105+ for (int z = 1 ;; ++z)
113106 {
114- for (int y = x; y <= z; ++y)
115- {
116- if (x*x + y * y == z * z)
107+ for (int x = 1 ; x <= z; ++x)
117108 {
118- result += (x + y + z);
119- ++found;
120- if (found == max_triples)
121- goto done;
109+ for (int y = x; y <= z; ++y)
110+ {
111+ if (x * x + y * y == z * z)
112+ {
113+ result += (x + y + z);
114+ ++found;
115+ if (found == max_triples)
116+ goto done;
117+ }
118+ }
122119 }
123- }
124120 }
125- }
126121done:
127- std::cout << t << ' \n ' ;
128- std::cout << result << ' \n ' ;
122+ std::cout << t << ' \n ' ;
123+ std::cout << result << ' \n ' ;
129124}
0 commit comments