File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Algorithms/Greedy Algorithms Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Author : Robin Singh
3
+ Implementation of Eqyption Fraction
4
+ Every positive fraction can be represented as sum of unique unit fractions
5
+ A fraction is unit fraction if numerator is 1 and denominator is a positive integer, for example 1/3 is a unit fraction
6
+ Such a representation is called Egyptian Fraction as it was used by ancient Egyptians
7
+ """
8
+
9
+ import math
10
+ def egyptianFraction (numerator , denominator ):
11
+ ef = []
12
+ while numerator != 0 :
13
+
14
+ x = math .ceil (denominator / numerator ) #Main
15
+ ef .append (x )
16
+ numerator = x * numerator - denominator
17
+ denominator = denominator * x
18
+ for i in range (len (ef )):
19
+ if i != len (ef ) - 1 :
20
+ print (f" 1/{ ef [i ]} +" ,end = " " )
21
+
22
+ else :
23
+ print (f"1/{ ef [i ]} " ,end = " " )
24
+
25
+ if __name__ == '__main__' :
26
+ nr = int (input ("Enter numerator" ))
27
+ dr = int (input ("Enter denominator" ))
28
+ print (f"Egyptian Fraction of { nr } /{ dr } : " )
29
+ egyptianFraction (nr ,dr )
You can’t perform that action at this time.
0 commit comments