-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm2.cpp
54 lines (51 loc) · 1.72 KB
/
algorithm2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <ratio>
#include <chrono>
#include <cmath>
#include <numeric>
#include <algorithm>
template<typename T>
struct PythagoreanTriple {
T a, b, c;
};
using Side = unsigned;
void get_triples(const Side limit_c, std::vector<PythagoreanTriple<Side>> &triples) {
for (Side c = 5; c != limit_c; ++c) {
for (Side b = 4; b != c; ++b) {
Side limit_a = sqrt(c * c - b * b) + 1;
for (Side a = c - b; a < limit_a && a < b; ++a) {
if (a * a + b * b == c * c && std::gcd(std::gcd(a, b), c) == 1) {
triples.emplace_back(a, b, c);
break;
}
}
}
}
}
int main(const int argc, const char **argv) {
Side limit_c;
if (argc == 2) {
limit_c = std::atoi(argv[1]) + 1;
}
else {
std::cout << "Maximum hypotenuse: ";
std::string str;
std::getline(std::cin, str);
limit_c = std::atoi(str.c_str()) + 1;
}
std::vector<PythagoreanTriple<Side>> triples;
auto time_point = std::chrono::high_resolution_clock::now();
get_triples(limit_c, triples);
auto time_elapsed = std::chrono::high_resolution_clock::now() - time_point;
std::cout << "Time elapsed: " << std::setprecision(12) << std::chrono::duration<double,std::micro>(time_elapsed).count() << "us\n";
std::cout << "Number of triples: " << triples.size() << "\nPress Enter to view...";
std::cin.get();
if (!std::cin.eof()) {
for (const auto& triple : triples) {
std::cout << '(' << triple.a << ',' << triple.b << ',' << triple.c << ')' << ' ';
}
}
}