-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathEgyptianFraction.cpp
47 lines (39 loc) · 941 Bytes
/
EgyptianFraction.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
// Program to print a fraction in Egyptian Form using Greedy Algorithm
/*
Example:Egyptian Fraction Representation of 6/14 is 1/3 + 1/11 + 1/231
Egyptian Fraction Representation of 12/13 is 1/2 + 1/3 + 1/12 + 1/156
*/
#include <iostream>
using namespace std;
void printEgyptian(int num, int dnum)
{
if (dnum == 0 || num == 0)
return;
if (dnum%num == 0)
{
cout << "1/" << dnum/num;
return;
}
if (num%dnum == 0)
{
cout << num/dnum;
return;
}
if (num > dnum)
{
cout << num/dnum << " + ";
printEgyptian(num%dnum, dnum);
return;
}
int n = dnum/num + 1;
cout << "1/" << n << " + ";
printEgyptian(num*n-dnum, dnum*n);
}
int main()
{
int num = 12, dnum= 13;
cout << "Egyptian Fraction Representation of "
<< num << "/" << dnum << " is\n ";
printEgyptian(num, dnum);
return 0;
}