Egyptian fraction solution in c++
C++ program for Egyptian fraction solution. Here more information.
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Egyptian fraction solution
*/
class Fraction
{
public:
void egyptianFraction(int a, int b)
{
if (a == 0 || b == 0)
{
return;
}
if (b >= a)
{
// Calculate remainder
int remainder = b % a;
// Calculate divisor
int divisor = b / a;
if (remainder != 0)
{
divisor++;
cout << "1/" << divisor << " + ";
a = a *divisor - b;
b = b *divisor;
this->egyptianFraction(a, b);
}
else
{
// When remainder is zero
cout << "1/" << divisor;
}
}
else
{
// When b < a
cout << (a / b) << " + ";
this->egyptianFraction(a % b, b);
}
}
};
int main()
{
Fraction *task = new Fraction();
// Two numbers
int a = 7;
int b = 53;
task->egyptianFraction(a, b);
return 0;
}
Output
1/8 + 1/142 + 1/30104
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment