Heronian triangles: Difference between revisions

→‎{{header|C++}}: C++11 to C++17 (basically just using std::gcd)
(add RPL)
(→‎{{header|C++}}: C++11 to C++17 (basically just using std::gcd))
Line 1,185:
 
=={{header|C++}}==
{{Works with|C++1117}}
<syntaxhighlight lang="cpp">#include <algorithmtuple>
#include <cmathvector>
#include <vectornumeric>
#include <iostream>
#include <tuplealgorithm>
#include <vector>
 
#include <cmath>
int gcd(int a, int b)
 
{
struct Triangle {
int rem = 1, dividend, divisor;
int a{};
std::tie(divisor, dividend) = std::minmax(a, b);
while (rem != 0)int b{};
int c{};
rem = dividend % divisor;
 
if (rem != 0) {
[[nodiscard]] constexpr auto perimeter() const noexcept { return a + b + c; }
dividend = divisor;
 
divisor = rem;
[[nodiscard]] constexpr auto area() const noexcept {
}
double const auto p_2 = static_cast<double>(perimeter(t)) / 2.;
double const auto area_sq = p_2 * ( p_2 - t.a ) * ( p_2 - t.b ) * ( p_2 - t.c );
return std::sqrt(area_sq);
}
return divisor;
}
 
struct Triangle
{
int a;
int b;
int c;
};
 
int perimeter(const Triangle& triangle)
{
return triangle.a + triangle.b + triangle.c;
}
 
double area(const Triangle& t)
{
double p_2 = perimeter(t) / 2.;
double area_sq = p_2 * ( p_2 - t.a ) * ( p_2 - t.b ) * ( p_2 - t.c );
return sqrt(area_sq);
}
 
std::vector<Triangle>auto generate_triangles(int side_limit = 200) {
{
std::vector<Triangle> result;
for(int a = 1; a <= side_limit; ++a)
for(int b = 1; b <= a; ++b)
for(int c = a + 1 - b; c <= b; ++c) // skip too-small values of c, which will violate triangle inequality
{
Triangle t{ a, b, c };
doubleconst auto t_area = t.area(t);
if (t_area == 0) continue;
if( (std::floor(t_area) == std::ceil(t_area) && std::gcd(a, std::gcd(b, c)) == 1)
result.push_back(t);
}
Line 1,241 ⟶ 1,224:
}
 
bool compare(const Triangle& lhs, const Triangle& rhs) noexcept {
return std::make_tuple(lhs.area(rhs), lhs.perimeter(rhs), std::max(rhslhs.a, std::max(rhslhs.b, rhslhs.c))); <
{
return std::make_tuple(rhs.area(lhs), rhs.perimeter(lhs), std::max(lhsrhs.a, std::max(lhsrhs.b, lhsrhs.c))) <;
std::make_tuple(area(rhs), perimeter(rhs), std::max(rhs.a, std::max(rhs.b, rhs.c)));
}
 
struct area_compare {
[[nodiscard]] constexpr bool operator()(int i, const Triangle& t, int i) const noexcept { return i < t.area(t) < i; }
{
[[nodiscard]] constexpr bool operator()(int i, const Triangle& t,) intconst i)noexcept { return area(t)i < it.area(); }
bool operator()(int i, const Triangle& t) { return i < area(t); }
};
 
int main() {
{
auto tri = generate_triangles();
std::cout << "There are " << tri.size() << " primitive Heronian triangles with sides up to 200\n\n";
Line 1,262 ⟶ 1,242:
std::cout << "area\tperimeter\tsides\n";
for(int i = 0; i < 10; ++i)
std::cout << area(tri[i].area() << '\t' << perimeter(tri[i].perimeter() << "\t\t" <<
tri[i].a << 'x' << tri[i].b << 'x' << tri[i].c << '\n';
 
Line 1,269 ⟶ 1,249:
std::cout << "area\tperimeter\tsides\n";
for(auto it = range.first; it != range.second; ++it)
std::cout << area(*it).area() << '\t' << perimeter(*it).perimeter() << "\t\t" <<
it->a << 'x' << it->b << 'x' << it->c << '\n';
}</syntaxhighlight>
11

edits