This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
forked from abhishekchopra13/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 52
/
EgyptianFractions.cpp
52 lines (47 loc) · 1.59 KB
/
EgyptianFractions.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
48
49
50
51
52
/* Algorithm Name-EgyptianFractions*/
/* Any fraction (numerator/denominator) can be expressed as a sum of positive distinct unit fractions. Unit fractions are fractions with numerator 1.
For example fraction 6/14 can be expressed as 1/3,1/11,1/231.
Procedure to find Egyptian Fraction is if numerator < denominator then first egyptian fraction is "1/quotient" where quotient is equal to Math.ceil(denominatoir/numerator).
The next the Egyptian Fraction will be founded by changing the original fraction (numerator/denominator) to numerator/denominator-1/quotient.
Sample Input
Enter numerator and denominator
6
14
Sample Output
1/3
1/11
1/231
*/
#include <iostream>
using namespace std;
int main()
{ int numerator,denominator,quotient;
cout<<"Enter numerator and denominator";
cout<<"\n";
cin>> numerator>>denominator;
while(numerator>denominator)
numerator=numerator%denominator;
while(denominator>numerator && numerator>0 && denominator>0)
{
if(numerator==1)
{
cout<<"1/"<<denominator;
cout<<"\n";
break;
}
else if(denominator==1)
break;
else
{
if(denominator%numerator==0)
quotient=denominator/numerator;
else
quotient=denominator/numerator+1;
cout<<"1/"<<quotient;
cout<<"\n";
numerator=numerator*quotient-denominator;
denominator=denominator*quotient;
}
}
return 0;
}