repl.it
@Yamette/

Noobie

Python

idk guan

fork
loading
Files
  • main.py
main.py
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
# Python3 program to print a fraction 
# in Egyptian Form using Greedy 
# Algorithm 

# import math package to use 
# ceiling function 
import math 

# define a function egyptianFraction 
# which receive parameter nr as 
# numerator and dr as denominator 
def egyptianFraction(nr, dr): 

	print("The Egyptian Fraction " +
		"Representation of {0}/{1} is". 
				format(nr, dr), end="\n") 

	# empty list ef to store 
	# denominator 
	ef = [] 

	# while loop runs until 
	# fraction becomes 0 i.e, 
	# numerator becomes 0 
	while nr != 0: 

		# taking ceiling 
		x = math.ceil(dr / nr) 

		# storing value in ef list 
		ef.append(x) 

		# updating new nr and dr 
		nr = x * nr - dr 
		dr = dr * x 

	# printing the values 
	for i in range(len(ef)): 
		if i != len(ef) - 1: 
			print(" 1/{0} +" . 
					format(ef[i]), end = " ") 
		else: 
			print(" 1/{0}" . 
					format(ef[i]), end = " ") 

# calling the function 
egyptianFraction(6, 14) 

# This code is contributed 
# by Anubhav Raj Singh