Greedy algorithm for Egyptian fractions: Difference between revisions
Greedy algorithm for Egyptian fractions (view source)
Revision as of 20:38, 13 January 2020
, 4 years ago→{{header|C++}}
m (C++ solution writes out numerator and denominator for Egyptian fractions with most terms/largest denominator) |
m (→{{header|C++}}) |
||
Line 202:
=={{header|C++}}==
The C++ standard library does not have a "big integer" type, so this solution uses the Boost library.
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <boost/multiprecision/cpp_int.hpp>
typedef boost::multiprecision::cpp_int integer;
integer mod(const integer& x, const integer& y)
Line 257 ⟶ 236:
}
{
while (x > 0)
{
Line 267 ⟶ 246:
y = y * z;
}
return result;▼
}
Line 289 ⟶ 267:
x = x % y;
}
std::vector<integer> result
print_egyptian(result);
}
Line 301 ⟶ 280:
integer max_denominator = 0;
std::vector<integer> max_denominator_result;
std::
for (integer b = 2; b < limit; ++b)
{
for (integer a = 1; a < b; ++a)
{
if (result.size() > max_terms)
{
|