Skip to content

Commit 63b8d0c

Browse files
authored
Merge pull request #606 from Asvenomdc/Asvenomdc-profile
Egyptian Fraction
2 parents 307a9c0 + 9c436e1 commit 63b8d0c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void printEgyptian(int nr, int dr)
5+
{
6+
// If either numerator or denominator is 0
7+
if (dr == 0 || nr == 0)
8+
return;
9+
10+
// If numerator divides denominator, then simple division
11+
// makes the fraction in 1/n form
12+
if (dr%nr == 0)
13+
{
14+
cout << "1/" << dr/nr;
15+
return;
16+
}
17+
18+
// If denominator divides numerator, then the given number
19+
// is not fraction
20+
if (nr%dr == 0)
21+
{
22+
cout << nr/dr;
23+
return;
24+
}
25+
26+
// If numerator is more than denominator
27+
if (nr > dr)
28+
{
29+
cout << nr/dr << " + ";
30+
printEgyptian(nr%dr, dr);
31+
return;
32+
}
33+
34+
// We reach here dr > nr and dr%nr is non-zero
35+
// Find ceiling of dr/nr and print it as first
36+
// fraction
37+
int n = dr/nr + 1;
38+
cout << "1/" << n << " + ";
39+
40+
// Recur for remaining part
41+
printEgyptian(nr*n-dr, dr*n);
42+
}
43+
44+
// Driver Program
45+
int main()
46+
{
47+
int nr = 6, dr = 14;
48+
cout << "Egyptian Fraction Representation of "
49+
<< nr << "/" << dr << " is\n ";
50+
printEgyptian(nr, dr);
51+
return 0;
52+
}

0 commit comments

Comments
 (0)